如何根据标签对帖子进行分组(WordPress)

时间:2011-08-08 07:04:32

标签: wordpress post blogs listings

我想在一个页面上列出所有帖子,但我希望帖子按标签分组。这在Wordpress中是否可行?

基本上就是这样:


自行车:

发表于2011年2月8日 一些帖子标题1 标签自行车

发表于2011年8月1日 一些帖子标题2 标签自行车


汽车:

发表于2011年5月8日 一些帖子标题5 标签车

发表于29/7/2011 一些帖子标题6 标签车


船:

发表于30/7/2011 一些帖子标题4 标记船


这可能吗?它应该是动态的,以便我可以从WP管理员创建新标签,它们会自动显示。

2 个答案:

答案 0 :(得分:2)

使用get_tags()循环使用标记,并使用带有'tag_in'参数的get_posts。

                <?php foreach(get_tags() as $term){ ?>

                    <?php $posts = get_posts( array( 'posts_per_page' => -1, 'post_type' => 'project', 'tag__in' => $term->term_id ) ); ?>

                    <?php if($posts) : ?>

                        <h3><?php echo $term->name; ?></h3>

                        <?php foreach($posts as $post) : ?>
                                <?php setup_postdata($post); ?>

                                <div class="item col-sm-12">
                                    <a href="<?php the_permalink(); ?>">
                                       <?php the_title(); ?>  <br/> 

                                    </a>
                                    <a class="button" href="<?php the_permalink() ?>">Read More</a>
                                </div>

                         <?php endforeach ?>

                         <?php wp_reset_postdata(); ?>

                    <?php endif; ?>

                <?php } ?>

答案 1 :(得分:0)

这取决于。您是要尝试显示所有代码的帖子,还是只列出您已经决定使用的特定代码的所有帖子?

如果你知道要显示的标签,可以按照标签列出帖子。

<?php $bikePosts = new WP_Query('tag=bikes');
while ($bikePosts->have_posts()) : $bikePosts->the_post(); ?>

<h2>Bikes:</h2>
<p>Posted <?php the_time('j/m/Y'); ?> <?php the_title(); ?>

<?php endwhile; 
//reset post data for next tag
wp_reset_postdata();
?>

更多信息:http://codex.wordpress.org/Class_Reference/WP_Query#Tag_Parameters

相关问题