wordpress query_posts特色页面始终位于顶部

时间:2010-11-11 21:13:52

标签: wordpress

我正在使用query_posts作为“Page”项而不是“Post”。我希望能够使“特色”页面始终位于顶部,我们可以添加一个名为“featured_product”的自定义字段,如果它是eq“1”,则将帖子显示为第一个。

以下是查询的基本代码。有人请帮忙!?

<?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; ?>

1 个答案:

答案 0 :(得分: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');

我推荐第一种方法之一,因为它将允许特色产品的自定义样式,并且实际上不会有任何缺点。我的网站每页都有四到五个自定义查询。

祝你好运。

相关问题