我只想在我的博客页面中显示5条帖子。有什么办法可以解决此问题?
您好nikita分页是wordpress的默认属性。请参阅Wordpress Codex,因此请从模板文件(index.php,category.php)中删除query_posts部分。
<?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' );
也许您的问题会解决...
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句