2012-11-27 24 views
0

我有一個循環來獲取按給定標籤分類的所有帖子(參見下面的代碼)。我需要把它轉過來,做一樣的事情,但通過給定類別的標籤。WordPresspress列出按給定分類標記分組的所有帖子

在代碼示例中,我獲取標記爲「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 ?> 

回答

0

明白了。這是tag_id=$tag_id的簡單變化而不是tag=$tag_id

上述代碼已更新。相關行是query_posts("tag_id=$tag_id&cat=eats&post_per_page=100");

相關問題