web-dev-qa-db-fra.com

in_category () et tax_query avec le type de publication personnalisé

J'utilise le type de message personnalisé, que j'ai nommé "produits". Pour ce type de message, j'ai ajouté les taxonomies personnalisées portant le nom "products_brand". Il contient deux termes: "natige" et "osakarovka".

Pour mon type de message personnalisé "produits", j'ai créé un modèle personnalisé, que j'ai appelé single-products.php.

Il est conçu de telle sorte que chaque terme de taxonomie ("natige" et "osakarovka") doit avoir un design différent.

J'ai essayé de le faire avec la fonction in_category () WP, mais cela ne fonctionne pas.

S'il vous plaît dites-moi, comment puis-je écrire mon modèle pour faire cet algorithme:

si la catégorie est "natige" elseif la catégorie est "osakarovka" fin

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

Dites-moi s'il vous plaît ce que j'ai à corriger.

Merci beaucoup!

1
Alex P.

Au lieu de in_category, essayez has_term - http://codex.wordpress.org/Function_Reference/has_term

Cela devrait fonctionner (je l'ai testé sous une forme plus simple sur mon hôte local):

<?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