0

我有一个循环,用于获取按给定标签的类别分组的所有帖子(请参见下面的代码)。我需要扭转它并做完全相同的事情,但通过给定类别的标签。

在代码示例中,我将所有帖子标记为“torrington”,然后执行循环以显示它们按类别分组,并带有类别的 H2(例如“餐厅”)。

所以反过来,我需要获取所有项目类别“餐厅”,然后按标签对它们进行分组(例如“torrington”、“danbury”等)。

<?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;

                // create a custom wordpress query
                query_posts("cat=$cat_id&tag=torrington&post_per_page=100");

                // start the wordpress loop!
                if (have_posts()) :     

                // Make a header for the category
                echo '<h2 class="cat-title">'.$cat->name.'</h2>';

                while (have_posts()) : 
                the_post(); ?>

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

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

编辑:我已经做到了这一点,但 query_posts 语句似乎什么也没返回:

 <?php           
        // get all the categories from the database
        $tags = get_tags(); 
            // loop through the categries
            foreach ($tags as $tag) {
                echo($tag->name);
                // setup the cateogory ID
                $tag_id = $tag->term_id;

                echo($tag_id);

                // create a custom wordpress query
                query_posts("tag_id=$tag_id&cat=eats&post_per_page=100");

                // start the wordpress loop!
                if (have_posts()) :     

                echo('posts');
                // Make a header for the category
                echo '<h2 class="cat-title">'.$tag->name.'</h2>';

                while (have_posts()) : 
                the_post(); ?>

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

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

1 回答 1

0

知道了。这是简单的改变tag_id=$tag_idnot tag=$tag_id

上面的代码更新了。相关线路是query_posts("tag_id=$tag_id&cat=eats&post_per_page=100");

于 2012-11-27T16:21:04.190 回答