web-dev-qa-db-ja.com

カスタム投稿タイプのin_category()およびtax_query

私は "products"という名前のカスタム投稿タイプを使用しています。この投稿タイプには "products_brand"という名前のカスタム分類法を追加しました。

私のカスタム投稿タイプ "products"用に、私はsingle-products.phpというカスタムテンプレートを作成しました。

それぞれの分類法の用語( "natige"と "osakarovka")は異なるデザインを持たなければならないということをとても想像している。

In_category()WP関数でこれをやろうとしましたが、うまくいきません。

このアルゴリズムを実行するためのテンプレートをどのように書くことができますか、教えてください。

カテゴリが "natige"の場合elseifカテゴリが "osakarovka"

これが私のsingle-products.phpコードです。

<?php get_header(); ?>

<div id="body">
<div class="fix">
<?php if (have_posts()) : while (have_posts()) : the_post(); ?>


<?php   
if ( in_category(array(

                'tax_query' => array(
                    'taxonomy' => 'products_brand',
                    'field' => 'slug',
                    'terms' => 'natige'
                ),
                'post_type' => 'products',
                'posts_per_page' => 1,
            )
        )
        ) :?>
    <div class="col-2 nati-col">
    <div class="col-left">
        <div class="nav">
        <a href="
        <?php 
        if (ICL_LANGUAGE_CODE == 'ru'){
            echo "/ru/produkciya-natizhe";
        } elseif (ICL_LANGUAGE_CODE == 'kz'){  
            echo "/natizhe-onimi";
        }
        ?>" class="natiact"><?php _e( 'Продукция «Н&#1241;тиже»', 'wpml_theme');?></a>
        <a href="
        <?php 
        if (ICL_LANGUAGE_CODE == 'ru'){
            echo "/ru/produkciya-osakarovka";
        } elseif (ICL_LANGUAGE_CODE == 'kz'){  
            echo "/osakarovka-onimi";
        }
        ?>" class="osahover"><?php _e( 'Продукция «Осакаровка»', 'wpml_theme');?></a>
        </div>

<div class="product-under">
<div class="pu-left"><?php the_post_thumbnail( 'product-page' ); ?>

<?php
if( get_field('dop_izobrazheniye') ): ?>
<img style="margin-top: 60px;" alt="<?php the_title_attribute(); ?>" title="<?php the_title_attribute(); ?>" src="<?php $image = get_field('dop_izobrazheniye'); echo($image['sizes']['product-page']); ?>"/>
<?php endif; ?>
</div>
<div class="pu-right"><h1><?php the_title(); ?></h1>

                <?php if( get_field('product_parameter') ): ?>

                    <?php while( has_sub_field('product_parameter') ): ?>

                            <?php if( get_sub_field('pack_volume_fat') ): ?>
                                <?php while( has_sub_field('pack_volume_fat') ): ?>
                                <ul class="prodparam">

                                    <li><b><?php the_sub_field('pack'); ?></b></li>
                                    <?php if (get_sub_field('mass_fat') ): ?>
                                        <li><?php while( has_sub_field('mass_fat') ): ?>

                                        <?php the_sub_field('mass'); ?>
                                        <?php the_sub_field('measure'); ?>&nbsp;&mdash;&nbsp;
                                        <?php the_sub_field('fatness'); ?></li>

                                        <?php endwhile; ?>
                                    <?php endif; ?>
                                </ul>
                                <?php endwhile; ?>

                            <?php endif; ?>

                    <?php endwhile; ?>

                <?php endif; ?>

            <?php the_content(); ?>


</div>
            <?php wp_reset_query(); ?>
<div class="clear"></div>
</div>

<hr><br>

<?php $orig_post = $post;
    global $post;
    $tags = wp_get_post_tags($post->ID);
    if ($tags) {
    $tag_ids = array();
    foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
    $args=array(
    'tag__in' => $tag_ids,
    'post__not_in' => array($post->ID),
    'post_type' => 'products',
    'posts_per_page'=>6, // Number of related posts that will be shown.
    'caller_get_posts'=>1
    );
    $my_query = new wp_query( $args );
    if( $my_query->have_posts() ) {


    echo _e( '<h2>Рекомендуем посмотреть</h2>', 'wpml_theme');

echo '<ul class="products">';
    while( $my_query->have_posts() ) {
    $my_query->the_post(); ?>

    <li>
        <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" alt="<?php the_title_attribute(); ?>"><i><?php the_post_thumbnail( 'product-cat' ); ?></i><?php the_title(); ?></a>
    </li>
    <? }
    }
    }
    $post = $orig_post;
    wp_reset_query(); ?>
    </ul>
    </div>
    <div class="col-right">
        <ul class="product-list">
        <?php $pc = new WP_Query(array(
        'order' => 'ASC', 
        'orderby' => 'title', 
        'posts_per_page' => '45', 
        'post_type' => 'products',
        'tax_query' => array(
                    array(
                    'taxonomy' => 'products_brand',
                    'field' => 'slug',
                    'terms' => 'natige'
                )
            )
        ));

        ?>
            <?php while ($pc->have_posts()) : $pc->the_post(); ?>
            <li>
            <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>
            </li>
        <?php endwhile; ?>
        <?php wp_reset_query() ?>
        </ul>
    </div>
    <div class="clear"></div>
</div>







<?php elseif ( in_category(array(

                'tax_query' => array(
                    'taxonomy' => 'products_brand',
                    'field' => 'slug',
                    'terms' => 'osakarovka'
                ),
                'post_type' => 'products',
                'posts_per_page' => 1,
            )
        )
        ) :?>
    <div class="col-2 osakarov-col">
    <div class="col-left">
        <div class="nav">
        <a href="
        <?php 
        if (ICL_LANGUAGE_CODE == 'ru'){
            echo "/ru/produkciya-natizhe";
        } elseif (ICL_LANGUAGE_CODE == 'kz'){  
            echo "/natizhe-onimi";
        }
        ?>" class="natihover"><?php _e( 'Продукция «Н&#1241;тиже»', 'wpml_theme');?></a>
        <a href="
        <?php 
        if (ICL_LANGUAGE_CODE == 'ru'){
            echo "/ru/produkciya-osakarovka";
        } elseif (ICL_LANGUAGE_CODE == 'kz'){  
            echo "/osakarovka-onimi";
        }
        ?>" class="osaact"><?php _e( 'Продукция «Осакаровка»', 'wpml_theme');?></a>
        </div>

<div class="product-under">
<div class="pu-left"><?php the_post_thumbnail( 'product-page' ); ?>

<?php
if( get_field('dop_izobrazheniye') ): ?>
<img style="margin-top: 60px;" alt="<?php the_title_attribute(); ?>" title="<?php the_title_attribute(); ?>" src="<?php $image = get_field('dop_izobrazheniye'); echo($image['sizes']['product-page']); ?>"/>
<?php endif; ?>
</div>
<div class="pu-right"><h1 class="osak-prod"><?php the_title(); ?></h1>

                <?php if( get_field('product_parameter') ): ?>

                    <?php while( has_sub_field('product_parameter') ): ?>

                            <?php if( get_sub_field('pack_volume_fat') ): ?>
                                <?php while( has_sub_field('pack_volume_fat') ): ?>
                                <ul class="prodparam">

                                    <li><b><?php the_sub_field('pack'); ?></b></li>
                                    <?php if (get_sub_field('mass_fat') ): ?>
                                        <li><?php while( has_sub_field('mass_fat') ): ?>

                                        <?php the_sub_field('mass'); ?>
                                        <?php the_sub_field('measure'); ?>&nbsp;&mdash;&nbsp;
                                        <?php the_sub_field('fatness'); ?></li>

                                        <?php endwhile; ?>
                                    <?php endif; ?>
                                </ul>
                                <?php endwhile; ?>

                            <?php endif; ?>

                    <?php endwhile; ?>

                <?php endif; ?>

            <?php the_content(); ?>


</div>
            <?php wp_reset_query(); ?>
<div class="clear"></div>
</div>

<hr><br>

<?php $orig_post = $post;
    global $post;
    $tags = wp_get_post_tags($post->ID);
    if ($tags) {
    $tag_ids = array();
    foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
    $args=array(
    'tag__in' => $tag_ids,
    'post__not_in' => array($post->ID),
    'post_type' => 'products',
    'posts_per_page'=>6, // Number of related posts that will be shown.
    'caller_get_posts'=>1
    );
    $my_query = new wp_query( $args );
    if( $my_query->have_posts() ) {


    echo _e( '<h2>Рекомендуем посмотреть</h2>', 'wpml_theme');

echo '<ul class="products">';
    while( $my_query->have_posts() ) {
    $my_query->the_post(); ?>

    <li>
        <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" alt="<?php the_title_attribute(); ?>"><i><?php the_post_thumbnail( 'product-cat' ); ?></i><?php the_title(); ?></a>
    </li>
    <? }
    }
    }
    $post = $orig_post;
    wp_reset_query(); ?>
    </ul>
    </div>
    <div class="col-right">
        <ul class="product-list">
        <?php $pc = new WP_Query(array(
        'order' => 'ASC', 
        'orderby' => 'title', 
        'posts_per_page' => '45', 
        'post_type' => 'products',
        'tax_query' => array(
                    array(
                    'taxonomy' => 'products_brand',
                    'field' => 'slug',
                    'terms' => 'osakarovka'
                )
            )
        ));

        ?>
            <?php while ($pc->have_posts()) : $pc->the_post(); ?>
            <li>
            <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>
            </li>
        <?php endwhile; ?>
        <?php wp_reset_query() ?>
        </ul>
    </div>
    <div class="clear"></div>
</div>

<?php endif;  ?>

<?php endwhile; else : ?>
<?php endif; ?>




</div>

</div>


<?php get_footer(); ?>

何を修正すればい​​いのか教えてください。

どうもありがとう!

1
Alex P.

In_categoryの代わりにhas_term - http://codex.wordpress.org/Function_Reference/has_term を試してください。

これはうまくいくはずです(私は自分のローカルホスト上でより単純な形式でそれをテストしました):

<?php get_header(); ?>

    <div id="body">
    <div class="fix">
    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>


    <?php   
    if ( has_term('natige', 'products_brand')) :?>
        <div class="col-2 nati-col">
        <div class="col-left">
            <div class="nav">
            <a href="
            <?php 
            if (ICL_LANGUAGE_CODE == 'ru'){
                echo "/ru/produkciya-natizhe";
            } elseif (ICL_LANGUAGE_CODE == 'kz'){  
                echo "/natizhe-onimi";
            }
            ?>" class="natiact"><?php _e( 'Продукция «Н&#1241;тиже»', 'wpml_theme');?></a>
            <a href="
            <?php 
            if (ICL_LANGUAGE_CODE == 'ru'){
                echo "/ru/produkciya-osakarovka";
            } elseif (ICL_LANGUAGE_CODE == 'kz'){  
                echo "/osakarovka-onimi";
            }
            ?>" class="osahover"><?php _e( 'Продукция «Осакаровка»', 'wpml_theme');?></a>
            </div>

    <div class="product-under">
    <div class="pu-left"><?php the_post_thumbnail( 'product-page' ); ?>

    <?php
    if( get_field('dop_izobrazheniye') ): ?>
    <img style="margin-top: 60px;" alt="<?php the_title_attribute(); ?>" title="<?php the_title_attribute(); ?>" src="<?php $image = get_field('dop_izobrazheniye'); echo($image['sizes']['product-page']); ?>"/>
    <?php endif; ?>
    </div>
    <div class="pu-right"><h1><?php the_title(); ?></h1>

                    <?php if( get_field('product_parameter') ): ?>

                        <?php while( has_sub_field('product_parameter') ): ?>

                                <?php if( get_sub_field('pack_volume_fat') ): ?>
                                    <?php while( has_sub_field('pack_volume_fat') ): ?>
                                    <ul class="prodparam">

                                        <li><b><?php the_sub_field('pack'); ?></b></li>
                                        <?php if (get_sub_field('mass_fat') ): ?>
                                            <li><?php while( has_sub_field('mass_fat') ): ?>

                                            <?php the_sub_field('mass'); ?>
                                            <?php the_sub_field('measure'); ?>&nbsp;&mdash;&nbsp;
                                            <?php the_sub_field('fatness'); ?></li>

                                            <?php endwhile; ?>
                                        <?php endif; ?>
                                    </ul>
                                    <?php endwhile; ?>

                                <?php endif; ?>

                        <?php endwhile; ?>

                    <?php endif; ?>

                <?php the_content(); ?>


    </div>
                <?php wp_reset_query(); ?>
    <div class="clear"></div>
    </div>

    <hr><br>

    <?php $orig_post = $post;
        global $post;
        $tags = wp_get_post_tags($post->ID);
        if ($tags) {
        $tag_ids = array();
        foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
        $args=array(
        'tag__in' => $tag_ids,
        'post__not_in' => array($post->ID),
        'post_type' => 'products',
        'posts_per_page'=>6, // Number of related posts that will be shown.
        'caller_get_posts'=>1
        );
        $my_query = new wp_query( $args );
        if( $my_query->have_posts() ) {


        echo _e( '<h2>Рекомендуем посмотреть</h2>', 'wpml_theme');

    echo '<ul class="products">';
        while( $my_query->have_posts() ) {
        $my_query->the_post(); ?>

        <li>
            <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" alt="<?php the_title_attribute(); ?>"><i><?php the_post_thumbnail( 'product-cat' ); ?></i><?php the_title(); ?></a>
        </li>
        <? }
        }
        }
        $post = $orig_post;
        wp_reset_query(); ?>
        </ul>
        </div>
        <div class="col-right">
            <ul class="product-list">
            <?php $pc = new WP_Query(array(
            'order' => 'ASC', 
            'orderby' => 'title', 
            'posts_per_page' => '45', 
            'post_type' => 'products',
            'tax_query' => array(
                        array(
                        'taxonomy' => 'products_brand',
                        'field' => 'slug',
                        'terms' => 'natige'
                    )
                )
            ));

            ?>
                <?php while ($pc->have_posts()) : $pc->the_post(); ?>
                <li>
                <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>
                </li>
            <?php endwhile; ?>
            <?php wp_reset_query() ?>
            </ul>
        </div>
        <div class="clear"></div>
    </div>







    <?php elseif ( has_term('osakarovka', 'products_brand')) :?>
        <div class="col-2 osakarov-col">
        <div class="col-left">
            <div class="nav">
            <a href="
            <?php 
            if (ICL_LANGUAGE_CODE == 'ru'){
                echo "/ru/produkciya-natizhe";
            } elseif (ICL_LANGUAGE_CODE == 'kz'){  
                echo "/natizhe-onimi";
            }
            ?>" class="natihover"><?php _e( 'Продукция «Н&#1241;тиже»', 'wpml_theme');?></a>
            <a href="
            <?php 
            if (ICL_LANGUAGE_CODE == 'ru'){
                echo "/ru/produkciya-osakarovka";
            } elseif (ICL_LANGUAGE_CODE == 'kz'){  
                echo "/osakarovka-onimi";
            }
            ?>" class="osaact"><?php _e( 'Продукция «Осакаровка»', 'wpml_theme');?></a>
            </div>

    <div class="product-under">
    <div class="pu-left"><?php the_post_thumbnail( 'product-page' ); ?>

    <?php
    if( get_field('dop_izobrazheniye') ): ?>
    <img style="margin-top: 60px;" alt="<?php the_title_attribute(); ?>" title="<?php the_title_attribute(); ?>" src="<?php $image = get_field('dop_izobrazheniye'); echo($image['sizes']['product-page']); ?>"/>
    <?php endif; ?>
    </div>
    <div class="pu-right"><h1 class="osak-prod"><?php the_title(); ?></h1>

                    <?php if( get_field('product_parameter') ): ?>

                        <?php while( has_sub_field('product_parameter') ): ?>

                                <?php if( get_sub_field('pack_volume_fat') ): ?>
                                    <?php while( has_sub_field('pack_volume_fat') ): ?>
                                    <ul class="prodparam">

                                        <li><b><?php the_sub_field('pack'); ?></b></li>
                                        <?php if (get_sub_field('mass_fat') ): ?>
                                            <li><?php while( has_sub_field('mass_fat') ): ?>

                                            <?php the_sub_field('mass'); ?>
                                            <?php the_sub_field('measure'); ?>&nbsp;&mdash;&nbsp;
                                            <?php the_sub_field('fatness'); ?></li>

                                            <?php endwhile; ?>
                                        <?php endif; ?>
                                    </ul>
                                    <?php endwhile; ?>

                                <?php endif; ?>

                        <?php endwhile; ?>

                    <?php endif; ?>

                <?php the_content(); ?>


    </div>
                <?php wp_reset_query(); ?>
    <div class="clear"></div>
    </div>

    <hr><br>

    <?php $orig_post = $post;
        global $post;
        $tags = wp_get_post_tags($post->ID);
        if ($tags) {
        $tag_ids = array();
        foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
        $args=array(
        'tag__in' => $tag_ids,
        'post__not_in' => array($post->ID),
        'post_type' => 'products',
        'posts_per_page'=>6, // Number of related posts that will be shown.
        'caller_get_posts'=>1
        );
        $my_query = new wp_query( $args );
        if( $my_query->have_posts() ) {


        echo _e( '<h2>Рекомендуем посмотреть</h2>', 'wpml_theme');

    echo '<ul class="products">';
        while( $my_query->have_posts() ) {
        $my_query->the_post(); ?>

        <li>
            <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>" alt="<?php the_title_attribute(); ?>"><i><?php the_post_thumbnail( 'product-cat' ); ?></i><?php the_title(); ?></a>
        </li>
        <? }
        }
        }
        $post = $orig_post;
        wp_reset_query(); ?>
        </ul>
        </div>
        <div class="col-right">
            <ul class="product-list">
            <?php $pc = new WP_Query(array(
            'order' => 'ASC', 
            'orderby' => 'title', 
            'posts_per_page' => '45', 
            'post_type' => 'products',
            'tax_query' => array(
                        array(
                        'taxonomy' => 'products_brand',
                        'field' => 'slug',
                        'terms' => 'osakarovka'
                    )
                )
            ));

            ?>
                <?php while ($pc->have_posts()) : $pc->the_post(); ?>
                <li>
                <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a>
                </li>
            <?php endwhile; ?>
            <?php wp_reset_query() ?>
            </ul>
        </div>
        <div class="clear"></div>
    </div>

    <?php endif;  ?>

    <?php endwhile; else : ?>
    <?php endif; ?>




    </div>

    </div>


    <?php get_footer(); ?>
2
Gabriela