【问题标题】:Wordpress list all posts grouped by tag for a given categoryWordpress 列出给定类别按标签分组的所有帖子
【发布时间】:2012-11-27 16:03:50
【问题描述】:

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

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

所以反过来,我需要获取所有项目类别“餐厅”,然后按标签对它们进行分组(例如“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 ?>

【问题讨论】:

    标签: php wordpress-theming wordpress


    【解决方案1】:

    知道了。这是tag_id=$tag_id 而不是tag=$tag_id 的简单更改。

    以上代码已更新。相关线路为query_posts("tag_id=$tag_id&amp;cat=eats&amp;post_per_page=100");

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-03-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-05-12
      • 1970-01-01
      相关资源
      最近更新 更多