2014-06-20 68 views
0

我通過第一個循環獲取category_name =「Products」的第一個列表。執行多重循環

而且the_content我得到這樣的:

例子:

一個

我已成立一個名爲$這裏給一個變量名

Ç

d = get_the_content()/ * $ product_category =「A」*/

現在我想執行一個內部循環來獲取category_name =「$ product_category」的嵌套列表。

這裏是我當前的代碼:

<?php query_posts('category_name=Products'); ?> 
       <?php while (have_posts()) : the_post(); ?> 
       <li> 
        <a> 
        <div style="padding:5px;background-color:black;margin-bottom:2px;"> 
        <div style="display:inline-block;width:100px;vertical-align:middle;"> 
        <?php if (has_post_thumbnail()) { ?> 
        <?php the_post_thumbnail(); ?> 
        <?php } ?> 
        </div> 
        <div style="display:inline-block;height:100%;"> 
         <a><?php the_title(); ?></a> 
        </div> 
        </div> 
        </a> 

        <?php $product_category = get_the_content(); ?> 
        <ul> 
        <?php query_posts('category_name=$product_category'); ?> 
        <?php while (have_posts()) : the_post(); ?> 
            <li> 
         <a><?php the_title(); ?></a> 
        </li> 
        <?php endwhile; ?> 
        </ul> 

       </li> 
       <?php endwhile; ?> 
       <?wp_reset_query(); ?> 

我的輸出只顯示第一個項目:

例子:

一個

如何使用多循環得到結果是:

例如:

一個

  • 產品1

  • 產品2

  • 產品3

Ç

  • product4的

d

  • 產品5

  • 產品6

對不起,我在WordPress中仍然是新的

+0

'$ PRODUCT_CATEGORY = get_the_content(); '將內容作爲分類名稱?這聽起來不對 –

+0

@PushpakPatel我還是在這裏新的。這個變量賦值方法我在Wordpress論壇網站上提及。我可以知道如何將「the_content」分配給變量嗎? – user3663143

回答

0

您正在查看的是標準查詢。您應該使用自定義帖子或自定義分類/自定義元來區分您的不同類別。除非你使用php來按名稱排序。

類WP_Query允許你搜索的數據庫,您可以發送

例如前將參數傳遞給類

$args=array(
     'post_type'=> array('product1','product2'), 
     'tax_query' => array(
     'relation'=>'AND', 
     array(
      'taxonomy'=> 'producttypes', 
      'field'=> 'name', 
      'terms'=> array('onsale', fullprice) 
     ) 

     array(
     'taxonomy' => 'brands', 
     'field' => 'name', 
     'terms' => array('cocacola', 'pepsi', 'gatorade'), 
    ) 
); 

$query= new wp_query($args); 

這裏有負載,你可以使用查詢變量,包括循環。對於多重循環,只需創建一個新的類實例,例如$ query2 = new wp_query($ newargs);這一切

參考: http://codex.wordpress.org/Class_Reference/WP_Query