繁体   English   中英

在index page wordpress中仅看到5篇最新文章

[英]see only 5 recent posts in index page wordpress

我该怎么做才能在wordpress的索引页面上仅看到5个最新帖子?

我有这个循环:

 <?php if (have_posts()) : while (have_posts()) : the_post(); ?>

我试图用它来解决它:

<?php
    $recentPosts = new WP_Query();
    $recentPosts->query('showposts=5');
?>
<?php while ($recentPosts->have_posts()) : $recentPosts->the_post(); ?>

它只返回错误。 我需要做什么?

请尝试这种方式:

<?php 
function five_posts_on_homepage( $query ) {
    if ( $query->is_home() && $query->is_main_query() ) {
        $query->set( 'posts_per_page', 5 );
    }
}
add_action( 'pre_get_posts', 'five_posts_on_homepage' );

if (have_posts()) : while (have_posts()) : the_post();
?>

如果有特殊原因,您必须编写代码? 否则,您实际上应该只使用“管理员设置”。

在设置->阅读下,有一个“最多显示博客页面”选项,您可以将其更改为5页(请参见下面的屏幕截图)。

在此处输入图片说明

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM