wordpress custom fields loop

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

我需要执行wordpress自定义字段循环:以下代码可以更好地描述:

<?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\' => \'difficulty\',
));



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

      echo \'<h2>"\'.$term->name . \'" Recipes</h2>\';
      
      echo \'<h4 class="tcount">\'. $total_results . \' results</h4>\';    ?>
      
     <hr class="aloha" />
           
    <?php order_properties_menu(); ?>
       
     <hr class="aloha" />
        
        
   <?php $counter = 0; ?>
   
<?php while ($query->have_posts()): $query->the_post();  ?>


    <div class="col-md-12 col-sm-6 col-xs-12">
                    <?php echo Chefscuisine_Template_Loader::load( \'recipes/grid\' ); ?>
        </div>
        
     
            
        <?php $counter++; endwhile; ?>
        
          <? if ($counter % 4 === 0) { 
                echo \'<div class="midad">Advert 336x280px Here!</div>\';
            } ?>
我得到了列表,但我现在需要的是tahonomy,例如,分类法模板中的平均难度,如何通过平均难度或轻松烹饪来实现这些都是自定义字段。它从类别模板中提取称为分类困难。php

1 个回复
SO网友:anton

我猜里面有数值recipe_star_rating 发布元数据。如果你有一个数值,试试这个,或者meta_value 如果存在字符串值。

$args = array(
    \'order\' => \'DESC\',
    \'meta_key\' => \'recipe_star_rating\',
    \'orderby\' => \'meta_value_num\' 
); 

$query = new WP_Query( $args );

if ( $query->have_posts() ) :
    $counter = 0;
    while ( $query->have_posts() ) :  $query->the_post(); 
        $counter++;
        the_title(); 
        echo "<br>"; 
    endwhile; 
    wp_reset_postdata();
endif;

相关推荐

Limit number of posts in loop

我知道如何把每页只有5篇文章分页。但假设我有4000个帖子,但我不想让人们看到我所有的帖子。我只想在4页中显示20篇文章(每页5篇)。$args = array( \'post_type\' => \'blog_posts\', \'posts_per_page\' => \'5\', ); $query = new WP_Query($args);