默认情况下,Wordpress会以相反的时间顺序显示所有帖子(首先是最新的帖子)。
我想显示我所有的wordpress文章的时间顺序(与最古老的帖子显示第一)。
我试图使用一个自定义循环查询来完成这个任务,但是我无法让它工作。我在这里错过了什么?
<?php query_posts(array('orderby'=>'date','order'=>'ASC'));
if ( have_posts() ) :
while ( have_posts() ) : the_post(); ?>
<div class="postTitle"><?php the_title(); ?></div>
<div class="postContent"><?php the_content(); ?></div>
<?php endwhile; endif;
wp_reset_query();
?>我认为这会很简单,虽然我所发现的一切尝试也不能使工作。谢谢!
发布于 2018-05-25 17:08:15
使用自定义循环的:
如果要创建自定义循环,则可能需要使用WP_Query。
<?php
$the_query = new WP_Query([
'order'=>'ASC'
]);
// The Loop
if ( $the_query->have_posts() ) :
while ( $the_query->have_posts() ) :
?>
<div class="postTitle"><?php the_title(); ?></div>
<div class="postContent"><?php the_content(); ?></div>
<?php
endwhile;
/* Restore original Post Data */
wp_reset_postdata();
?>
<?php else: ?>
// no posts found
<?php endif; ?>使用过滤器的
或者另一种方法是使用functions.php文件中的过滤器更改主循环。
function alter_order_of_posts( $query ) {
if ( $query->is_main_query() ) {
$query->set( 'order', 'ASC' );
}
}
add_action( 'pre_get_posts', 'alter_order_of_posts' );我建议使用过滤器路径,以避免更改当前模板的很多内容。
https://stackoverflow.com/questions/50533759
复制相似问题