in_category() and tax_query with custom post type

I’m using custom post type, that i have named “products”
For this post type i have add the custom taxonomies, that have name “products_brand”
There are two terms in it now: “natige” and “osakarovka”

For my custom post type “products” I have created a custom template, that I called single-products.php

Read More

It’s so conceived, that each taxonomy term (“natige” and “osakarovka”) must have different design.

I tried to do this with in_category() WP function, but it doesn’t work.

Please tell me, how can I write my template to do this algorithm:

if category is “natige”

elseif category is “osakarovka”

end

Here is my single-products.php code:

<?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( 'Продукция «Нәтиже»', '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( 'Продукция «Нәтиже»', '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(); ?>

Tell me please what have I to correct.

Thanks a lot!

Related posts

1 comment

  1. Instead of in_category try has_term – http://codex.wordpress.org/Function_Reference/has_term

    This should work (I’ve tested it in a simpler form on my localhost):

    <?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(); ?>
    

Comments are closed.