Filter posts by category

时间:2016-06-04 作者:nick cruse

如何从自定义类别页面上的特定类别获取帖子?

下面的代码获取所有类别的帖子,但我找不到根据特定类别进行筛选的方法。有人能告诉我按特定类别过滤的正确方向吗。

<?php // Display blog posts on any page @ http://m0n.co/l
        $temp = $wp_query; $wp_query= null;
        $wp_query = new WP_Query(); $wp_query->query(\'showposts=2\' . \'&paged=\'.$paged);
        while ($wp_query->have_posts()) : $wp_query->the_post(); ?>

        <div class="col-md-6 col-post-blog wow fadeInUp">

        <div class="post-blog">

        <?php the_post_thumbnail(\'large\'); ?>

        <h3><a href="<?php the_permalink(); ?>" title="Read more"><?php the_title(); ?></a></h3>
        <?php the_excerpt(); ?>

        <p><span class="fa fa-clock-o"></span> <?php the_time(\'F j, Y\') ?></p>
         </div><!--col-md-6-->
         </div><!--post-blog-->

        <?php endwhile; ?>

        <?php if ($paged > 1) { ?>

        <ul class="pagination pagination-lg">

            <li><?php next_posts_link(\'&laquo; Previous Posts\'); ?></li>
            <li><?php previous_posts_link(\'Newer Posts &raquo;\'); ?></li>
        </ul>

        <?php } else { ?>

        <ul class="pagination pagination-lg">

            <li><?php next_posts_link(\'&laquo; Previous Posts\'); ?></li>
            <li class="disabled"><a>Newer Posts &raquo;</a></li>
        </ul>

        <?php } ?>

        <?php wp_reset_postdata(); ?>

1 个回复
SO网友:thebigtine

根据WP Query Codex page 您只需将wp查询更改为:

$wp_query = new WP_Query( array( \'cat\' => $cat_id ) );