首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >按时间顺序显示Wordpress文章(上升)

按时间顺序显示Wordpress文章(上升)
EN

Stack Overflow用户
提问于 2018-05-25 16:52:37
回答 1查看 2.7K关注 0票数 0

默认情况下,Wordpress会以相反的时间顺序显示所有帖子(首先是最新的帖子)。

我想显示我所有的wordpress文章的时间顺序(与最古老的帖子显示第一)。

我试图使用一个自定义循环查询来完成这个任务,但是我无法让它工作。我在这里错过了什么?

代码语言:javascript
运行
复制
<?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();
    ?>

我认为这会很简单,虽然我所发现的一切尝试也不能使工作。谢谢!

EN

回答 1

Stack Overflow用户

发布于 2018-05-25 17:08:15

使用自定义循环的

如果要创建自定义循环,则可能需要使用WP_Query

代码语言:javascript
运行
复制
<?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文件中的过滤器更改主循环。

代码语言:javascript
运行
复制
function alter_order_of_posts( $query ) {
    if ( $query->is_main_query() ) {
        $query->set( 'order', 'ASC' );
    }
}
add_action( 'pre_get_posts', 'alter_order_of_posts' );

我建议使用过滤器路径,以避免更改当前模板的很多内容。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/50533759

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档