只显示某一类别的帖子?

时间:2012-04-27 作者:Rob

如何仅显示特定类别(类别51)中的项目?

下面是我的循环,我需要将代码合并到其中。

<div class="news-content-inner">
  <?php $portfolioloop = new WP_Query( array( \'paged\' => get_query_var(\'paged\'), \'post_type\' => \'news\', \'posts_per_page\' => 4 ) ); ?>
  <?php while ( $portfolioloop->have_posts() ) : $portfolioloop->the_post(); ?>
  <div class="news-item" onClick="location.href=\'<?php echo the_permalink(); ?>\'">
    <h2><a style="color:#F45B11!IMPORTANT;" href="<?php echo the_permalink(); ?>"><?php echo the_time(\'d.m.Y\'); ?> / <?php echo the_title(); ?></a></h2>
    <p class="news-page">
      <?php if (get_field(\'description\') != "") { ?>
      <?php echo the_field(\'description\'); ?>
        <?php } else { 
        $newscontent = get_the_content();
        $newscontent_str = strip_tags($newscontent, \'\');
        echo substr($newscontent_str,0,250) . "…";
        } ?>
    </p>
  </div>
  <?php endwhile; // end of the loop. ?>
  <p class="news-page" style="font-size:12px!IMPORTANT;"><?php echo wp_count_posts(\'news\')->publish; ?> opportunities</p>
    <?php if (function_exists(\'wp_pagenavi\')) {
    wp_pagenavi( array( \'query\' => $portfolioloop ) ); } ?>
</div>

1 个回复
最合适的回答,由SO网友:Geert 整理而成

您需要修改WP_Query 环添加一个tax_query 参数筛选所需类别。

$portfolioloop = new WP_Query(array(
    \'paged\'          => get_query_var(\'paged\'),
    \'post_type\'      => \'news\',
    \'posts_per_page\' => 4,
    \'tax_query\'      => array(
        // Note: tax_query expects an array of arrays!
        array(
            \'taxonomy\' => \'portfolio\', // my guess
            \'field\'    => \'id\',
            \'terms\'    => 51,
        ),
    ),
));

http://codex.wordpress.org/Class_Reference/WP_Query#Taxonomy_Parameters

结束

相关推荐

Thesis Theme Custom Loop

我正在使用论文主题构建一个网站,并使用论文自定义循环API和自定义WP\\U查询。当我在单页上执行此操作时,它不会显示评论表单。如何在单个帖子页面上添加评论表单