2010-10-28 66 views
1

我有一個我正在使用的模板,可以選擇在精選部分的首頁顯示一組帖子或者可選地,在相同的特色區域中顯示一組指定的頁面。我發現代碼顯示的是/或,但是我不確定如何將兩者結合在一起,並獲得一組帖子和頁面的列表。如何使用Wordpress的query_posts()方法顯示一組組合帖子和頁面

我的理解是,query_posts()會覆蓋Wordpress在頁面上顯示的任何項目集合,因此這裏根據主題所處的模式,它將參數傳遞給query_posts()以獲取特定類別的帖子,或通過在頁面的數組:

<div id="slides"> 
    <?php global $ids; 
    $ids = array(); 

    $featured_cat = get_option('mytemplate_feat_cat'); 
    $featured_num = get_option('mytemplate_featured_num'); 

    if (get_option('mytemplate_use_pages') == 'false') query_posts("showposts=$featured_num&cat=".get_cat_ID($featured_cat)); 
    else { 
     global $pages_number; 

     if (get_option('mytemplate_feat_pages') <> '') $featured_num = count(get_option('mytemplate_feat_pages')); 
     else $featured_num = $pages_number; 

     query_posts(array 
         ('post_type' => 'page', 
         'orderby' => 'menu_order', 
         'order' => 'ASC', 
         'post__in' => get_option('mytemplate_feat_pages'), 
         'showposts' => $featured_num 
        )); 
    } ?> 
      <!-- Start my loop to display everything--> 
    <?php if (have_posts()) : while (have_posts()) : the_post(); 
    global $post; ?> 

到目前爲止,我做了它多一點consise,但在如何將參數說query_posts結合最後一點不能得到(getMyPostsArray ().AddList(ohINeedACouplePagesToo()))//是的,我知道這看起來像C#或什麼...我不是一個PHP的傢伙..

這裏稍微更可讀的版本,是更接近我想要的代碼:

  $featured_cat = get_option('mytemplate_feat_cat'); 
       //I combined featured_num to get the total number of featured items to display 
     $featured_num = get_option('mytemplate_featured_num') + count(get_option('mytemplate_feat_pages'));; 

query_posts("showposts=$featured_num&cat=".get_cat_ID($featured_cat)); 

      //I think this second line overwrites the first query_posts() :-/ 
      query_posts(array 
          ('post_type' => 'page', 
          'orderby' => 'menu_order', 
          'order' => 'ASC', 
          'post__in' => get_option('mytemplate_feat_pages'), 
          'showposts' => $featured_num 
         )); 

回答

1

瑞恩,你爲什麼不乾脆去有兩個查詢和兩個循環?

query_posts("post_type=post&showposts=3"); 
while (have_posts()) { the_post(); } 

query_posts(array(
    "post_type" => "page", 
    "post__in" => get_option("mytemplate_feat_pages"), 
    "showposts" => 5 
)); 
while (have_posts()) { the_post(); } 

如果你需要一定數量的文章和網頁的填補空間,你也可以使用$wp_query->found_posts去實際計算你有什麼,你需要什麼。這可能是最簡單的解決方案,因爲即使您可以在SQL中獲取帖子和頁面,也可能很難排序,因爲帖子沒有菜單順序,而您不喜歡按發佈日期排序的頁面。

希望有幫助, 乾杯!

+0

運行兩次就像一個魅力。出於某種原因,我認爲query_posts只能在每頁運行一次。我只跑了兩次,一次是發帖,然後是頁面。謝謝! – 2010-10-29 03:50:06

相關問題