2011-10-09 61 views
1

列表的帖子在下面的代碼有一個循環,具有所有的產品在一個給定的類別:PHP/WordPress的:每個子類

 <?php   

      wp_reset_query(); 
      query_posts($query_string . '&posts_per_page=15&paged=' . $paged); 
      if (have_posts()) : 
      while (have_posts()) : the_post(); 
       $price = get_post_meta(get_the_ID(), 'inception_price', true); 

     ?> 

      <div class="omc-product-listing"> 
       <a href="<?php the_permalink();?>"> 
        <?php 

         if(has_post_thumbnail()) { 
         the_post_thumbnail('product', array('class' => 'omc-product-frame')); 
         } else { 
        ?> 

        <img src="<?php echo get_template_directory_uri() ;?>/images/no-image.png" width="170" height="170" class="omc-product-frame" alt="no photo" /> 

        <?php } ?> 

       </a> 

       <span class="omc-listing-header"><a href="<?php the_permalink();?>"><?php the_title();?></a></span> 

       <span class="omc-listing-price"><?php echo($price);?></span> 

       <span class="omc-listing-more"><a href="<?php the_permalink();?>">&raquo;</a></span> 

      </div><!-- /omc-product-listing --> 

     <?php endwhile; ?> 

     <br class="clear" /> 

     <div class="product-pagination"> 

      <?php kriesi_pagination(); ?>  

     </div>       

     <br class="clear" /> 

     <?php endif; wp_reset_query(); ?> 

在這個循環中,我想拉出來的每一件產品的給定類別(即現在的代碼做什麼),但 「每個子類別」 然後顯示,像這樣:

對於A類出版物:

書籍:

  • 簿33
  • 簿32
  • 書1
  • ...

移動應用:

  • 應用12
  • 應用76
  • ...

...

我認爲上面的代碼需要一個foreach循環,如下所示,但我不知道如何在這種情況下實現它。

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

       // loop through the categories 
       foreach ($cats as $cat) { 
        // setup the categories ID 
        $cat_id= $cat->term_id; 
        // Make a header for the categories 
        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 ?> 

回答

2

我已將此請求轉到wpquestions.com。它由Abdessamad Idrissi解決。答案在這裏複製太長,所以我在這裏發佈discussion linkcode link,以防某人有相同的需求。

+0

+1供您自學併爲他人提供鏈接。繼續走下去。 –

+0

謝謝Awais,非常感謝。代碼的最後一個版本[這裏](http://pastebin.com/cb5e9egt)。 –

1

你可以嘗試以下方法:

$cats = get_categories(); 

foreach ($cats as $cat) : 

// setup the categories ID 
$cat_id= $cat->term_id; 
// Make a header for the categories 
echo "<h2>".$cat->name."</h2>"; 

$args = array('cat' => $cat_id, 'posts_per_page' => 100); 

$posts = get_posts($args); 

if($posts) : 

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

<a href="<?php the_permalink();?>"><?php the_title(); ?></a> 

<?php endforeach; // foreach($posts) 

endif; // if($posts) 

endforeach; // foreach($cats) 

沒有測試,但它應該讓你在正確的方向!

+0

感謝戴夫,看到我的意見以下。乾杯。 –

相關問題