[英]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.