我想在我的博客页面中只显示5个帖子。有没有办法解决这个问题?
答案 0 :(得分:1)
嗨nikita分页是wordpress的默认属性。 参考Wordpress Codex 所以请从模板文件中删除query_posts部分(index.php,category.php)。
<?php
// query to set the posts per page to 5
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args = array('posts_per_page' => 5, 'paged' => $paged );
query_posts($args); ?>
将主页和类别页面的查询添加回主题的 functions.php 文件中:
function my_post_queries( $query ) {
// do not alter the query on wp-admin pages and only alter it if it's the main query
if (!is_admin() && $query->is_main_query()){
// alter the query for the home and category pages
if(is_home()){
$query->set('posts_per_page', 5);
}
if(is_category()){
$query->set('posts_per_page', 5);
}
}
}
add_action( 'pre_get_posts', 'my_post_queries' );
也许你的问题会解决......