首页 > wordpress-theming > wordpress每页不同的帖子数量

wordpress每页不同的帖子数量 (wordpress different posts per page qty)

问题

wordpress在主页上显示了最近的帖子。设置为每页18个帖子。如何制定一些规则以在第一页(已配置)上显示18个帖子,在第二页和后续页面上显示8个帖子?

<?php while ( have_posts() ) : the_post();  
    get_template_part( 'content', get_post_format() );
endwhile; ?>

尝试了以下内容:

add_filter( 'pre_get_posts', 'my_get_posts' );
function my_get_posts( $query ) {
if ($query->is_main_query()){
    if (is_home()){
        $query->set('showposts', 18);
    }
    else {
        $query->set('showposts',7);
        $query->set('posts_per_page',7);
    }
}
return $query;
}

没有效果 :-(

解决方法

您可以使用函数is_home()来检查您的位置:http://codex.wordpress.org/Function_Reference/is_home

在您的PHP功能中,如果您不在家中,您可以更改每页的帖子编号,但它可能会在您的导航功能上出现一些问题。

你试过这个插件吗? http://wordpress.org/extend/plugins/custom-posts-per-page/

它可能包含一些有趣的代码来解决您的问题。

问题

My wordpress shows recent posts on the home page. Settings are 18 posts per page. How can I make some rule to show 18 posts on the first page (as configured) and 8 posts on the second and following pages?

<?php while ( have_posts() ) : the_post();  
    get_template_part( 'content', get_post_format() );
endwhile; ?>

Have tried the following:

add_filter( 'pre_get_posts', 'my_get_posts' );
function my_get_posts( $query ) {
if ($query->is_main_query()){
    if (is_home()){
        $query->set('showposts', 18);
    }
    else {
        $query->set('showposts',7);
        $query->set('posts_per_page',7);
    }
}
return $query;
}

No effect :-(

解决方法

You can use the function is_home() to check where you are : http://codex.wordpress.org/Function_Reference/is_home

In your php function if your not in home you can change the post number per page, but it could make some issues on your navigation function.

Have you tried this plugin? http://wordpress.org/extend/plugins/custom-posts-per-page/

It could contains some interesting code to resolve your problem.

相似信息