是否可以在保持标准Wordpress循环不变的情况下订购帖子(即不必创建一个全新的WP_Query )?
我所说的标准循环是指:
<?php if ( have_posts() ) : ?>
<?php /* The loop */ ?>
<?php while ( have_posts() ) : the_post(); ?>我可以在此代码中指定顺序吗?
发布于 2013-10-27 13:24:59
作为记录在案在query_posts功能页面上:
强烈建议您使用
pre_get_posts筛选器,并通过检查is_main_query来更改主查询。
您可以在主题pre_get_posts文件中添加一个新的functions.php操作,例如:
function homepage_posts($query)
{
if ($query->is_home() && $query->is_main_query())
{
$query->set( 'orderby', 'title' );
}
}
add_action('pre_get_posts', 'homepage_posts');发布于 2013-10-27 13:15:42
查询()是要走的路
示例片段
<?php query_posts(array('orderby'=>'title','order'=>'DESC'));
if ( have_posts() ) :
while ( have_posts() ) : the_post(); ?>
<a href="<?php the_permalink() ?>"><?php the_title() ?></a><br /><?php
endwhile;
endif;
wp_reset_query();但请记住: query_posts()将更改您的主查询,因此不建议使用。只在绝对必要时使用(请参阅query_posts:注意事项)。创建WP_Query或get_posts()的新实例是次要循环的首选。
https://stackoverflow.com/questions/19618235
复制相似问题