How to read taxonomy in loop?

时间:2021-08-13 作者:Djordje Milivojevic

如何在循环中读取分类法?下面是更好地解释的代码:

    <?php $term = get_term_by(\'slug\', get_query_var( \'term\' ), get_query_var( \'taxonomy\' ) );

 $query = new WP_Query(array(
    \'post_type\' => \'recipe\',
    \'meta_key\' => \'recipe_star_rating\',
    \'orderby\' => \'meta_value_num\',
    \'order\' => \'DESC\',
   /// \'taxonomy\' => $term->slug,
));

$counter = 0;



 global $wp_query;
       
      $total_results = $query ->found_posts;

      echo \'<h2>"\'.$term->name . \'" Recipes</h2>\';
      
      echo \'<h4 class="tcount">\'. $total_results . \' results</h4>\';    ?>
      
     <hr class="aloha" />

<?php while ($query->have_posts()): $query->the_post(); $counter++;  ?>


    <div class="col-md-12 col-sm-6 col-xs-12">
                    <?php echo Chefscuisine_Template_Loader::load( \'recipes/grid\' ); $counter++; ?>
    </div>
        
     
            
        <?php endwhile; ?>
        
          <? if ($counter % 4 === 0) { 
                echo \'<div class="midad">Advert 336x280px Here!</div>\';
            } ?>
所以我得到了清单,但并没有阅读困难-它可以是简单的,困难的,一般的。

1 个回复
SO网友:Djordje Milivojevic

我知道了,代码是:

...

<?php 

$term = get_term_by(\'slug\', get_query_var( \'term\' ), get_query_var( \'taxonomy\' ), get_queried_object()->term_id );

// $paged = ( get_query_var( \'paged\' ) ) ? get_query_var( \'paged\' ) : 1;

$query = new WP_Query(array(
    \'post_type\' => \'recipe\',
    \'meta_key\' => \'recipe_star_rating\',
    \'orderby\' => \'meta_value_num\',
    \'order\' => \'DESC\',
   //  \'paged\' => $paged,
    \'status\' => get_query_var( \'term\' ),
));

global $wp_query;

$total_results = $query ->found_posts;

echo \'<h2>"\'.$term->name . \'" Recipes</h2>\';
      
echo \'<h4 class="tcount">\'. $total_results . \' results</h4>\';  
      
?>

<hr class="aloha" />
    
<?php $counter = 0;  while ($query->have_posts()): $query->the_post(); $counter++;  ?>

<div class="col-md-12 col-sm-6 col-xs-12">
<?php echo Chefscuisine_Template_Loader::load( \'recipes/grid\' ); // $counter++; ?>
</div>
        
<? if ($counter % 3 === 0) { 
                echo \'<div class="midad">Advert 336x280px Here!</div>\';
} ?>
            
            
<?php endwhile; ?>

</div>
...

相关推荐

如果(!$loop->Have_Posts())条件不起作用-WP_QUERY

所以我几乎可以肯定的是,我让它工作得很好,然后不知从哪里出来,它似乎没有工作。我到处修补,让它把一切都隐藏起来,然后什么也不隐藏。想知道是否有人发现我做错了什么?下面的代码基本上可以扫描我的所有产品-simple & variable - 看看是否有IN STOCK 产品有sale 它们的价格。如果没有IN STOCK 具有的产品sale 价格,隐藏某个CSS ID。否则,什么也不做。不知道我错过了什么。。。// -----------------------------------------&