2010-11-11 40 views
1

我使用query_posts作爲「Page」項目而不是「Post」。我希望能夠將「精選」頁面始終放在最前面,我們可以添加一個名爲「featured_product」的自定義字段,如果它是「1」,則將該帖子顯示爲第一個。wordpress query_posts精選頁面總是在頂部

下面是查詢的基本代碼。有人請幫忙!?

<?php query_posts(array('showposts' => 1000, 'post_parent' => $post->ID, 'post_type' => 'page', 'orderby' => 'title', 'order' => 'ASC')); ?> 
<?php if (have_posts()) : while (have_posts()) : the_post(); ?> 
...        
<?php endwhile; else: ?> 
... 
<?php endif; ?> 
+0

這是在http://wordpress.stackexchange.com/上提出的一個很好的問題。 – 2010-11-11 21:22:22

回答

2

您可以在給定頁面上運行任意數量的query_posts()。這給你幾個選擇,以顯示特色頁面,然後定期頁面。

一種方式和我經常在我的WordPress網站上組織內容的方式是擁有一個「精選」類別。精選的帖子或頁面會與其他適當的類別一起分配給此類別。然後,在query_posts()或get_posts()查詢字符串中使用該類別名稱或ID。

//get featured posts (category_ID = 13) 
query_posts('cat=13&post_type=page'); 
//the loop goes here 

另一種方法是在查詢字符串中使用meta_key和meta_value。

query_posts('meta_key=featured_product&meta_value=1&post_type=page'); 
//the loop goes here 

這兩種方法假定您查詢並顯示您精選的網頁後,您會被排除(13在這個例子中)分配到精選文章類別或使用meta_compare如果自定義字段查詢您的標準頁使用

//get non-featured posts 
query_posts('cat=-13&post_type=page') 
//or 
query_posts('meta_key=featured_product&meta_compare=!=&meta_value=1'); 
//the loop goes here 

如果你想要做一切查詢和按特色狀態排序,我推測你可以通過meta_value命令。

//this might order by meta_value, i didn't test it. 
query_posts('post_type=page&orderby=meta_value_num&order=asc'); 

我推薦的第一方法之一,因爲它將使特色產品的定製造型,真的不會有任何缺點。我在每個網頁上都有四個或五個自定義查詢的網站。

好運。