Wordpress按类别列出与标签匹配的帖子?

时间:2012-11-25 15:53:24

标签: php wordpress-theming wordpress

我需要按类别列出帖子,但仅限于具有给定标记(或标记)的帖子。到目前为止,我有以下代码,适用于列出按类别分组的所有帖子,但我不知道如何修改它只选择具有给定标签的帖子。我应该改变什么?

 <?php           
        // get all the categories from the database
        $cats = get_categories(); 

            // loop through the categries
            foreach ($cats as $cat) {
                // setup the cateogory ID
                $cat_id= $cat->term_id;
                // Make a header for the cateogry
                echo "<h2>".$cat->name."</h2>";
                // create a custom wordpress query
                query_posts("cat=$cat_id&post_per_page=100");
                // start the wordpress loop!
                if (have_posts()) : while (have_posts()) : the_post(); ?>

                    <?php // create our link now that the post is setup ?>
                    <a href="<?php the_permalink();?>"><?php the_title(); ?></a>
                    <?php echo '<hr/>'; ?>

                <?php endwhile; endif; // done our wordpress loop. Will start again for each category ?>
            <?php } // done the foreach statement ?>

1 个答案:

答案 0 :(得分:1)

您需要做的就是编辑您的查询以包含标记,即 query_posts("cat=$cat_id&tag=tag1+tag1&showposts=100");

请注意,您需要全部tag1+tag1