0

I have Jetpack installed and I have got Infinite Scroll working on my theme but posts_per_page is having no effect, the number of posts initially shown and loaded is my "Blogs show at most" number in Settings → Reading which is 5.

function mytheme_render_infinite_scroll() {
    while ( have_posts() ) : the_post();
            if ('mytheme_custom_post_type' == get_post_type()) :
                get_template_part( 'content', 'archive-custom' );
            else :
                get_template_part( 'content', get_post_format() );
            endif;
    endwhile;
}

function mytheme_jetpack_setup() {
    add_theme_support( 'infinite-scroll', array(
        'container'      => 'content',
        'footer_widgets' => 'footer-widgets',
        'type'           => 'click',
        'render'         => 'mytheme_render_infinite_scroll',
        'wrapper'        => true,
        'posts_per_page' => '15'
    ) );
}

I am also rendering a different content template depending on what the current post type is using the mytheme_render_infinite_scroll function above. It would be great if I could set the posts_per_page value to be different depending on post type too, eg. have 5 posts per page show on a Blog archive but have 15 posts per page show on an archive page of mytheme_custom_post_type. I am not sure how to do this, my attempt would be:

function mytheme_render_infinite_scroll() {
    while ( have_posts() ) : the_post();
            if ('mytheme_custom_post_type' == get_post_type()) :
                get_template_part( 'content', 'archive-custom' );
            else :
                get_template_part( 'content', get_post_format() );
            endif;
    endwhile;
}

function mytheme_infinite_scroll_posts_per_page()
    while ( have_posts() ) : the_post();
        if ('mytheme_custom_post_type' == get_post_type()) :
            return 15;
        else :
            return 5;
        endif;
endwhile;

function mytheme_jetpack_setup() {
    add_theme_support( 'infinite-scroll', array(
        'container'      => 'content',
        'footer_widgets' => 'footer-widgets',
        'type'           => 'click',
        'render'         => 'mytheme_render_infinite_scroll',
        'wrapper'        => true,
        'posts_per_page' => 'mytheme_infinite_scroll_posts_per_page'
    ) );
}
add_action( 'after_setup_theme', 'mytheme_jetpack_setup' );

... but I have no idea if this would work, Jetpack is probably not set up to handle this! Can't test it either because as I said above the posts_per_page argument isn't having any effect.

The Jetpack support doc is here: http://jetpack.me/support/infinite-scroll/

EDIT: I've done some testing and the conclusion I've come to is Jetpack is built so that the posts_per_page argument only has an effect if the type is not set as click. I can't find this specifically stated in any documentation so I may be wrong but I've got posts_per_page working with 'type' => 'scroll' but not with 'type' => 'click'

  • Does it work if you omit the posts_per_page argument in the Jetpack setup function? – s_ha_dum Dec 20 '13 at 15:47
  • Yes, Jetpack Infinite scroll in general is working, whether or not posts_per_page is there, but the only thing that seems to affect the number of posts that are loaded is the value entered in Settings > Reading. I can't test my idea for mytheme_infinite_scroll_posts_per_page (although I highly doubt it will work) because it depends on the posts_per_page argument working. Not sure if I understand the question? – Sarah Dec 20 '13 at 15:57
0

The plugin author has confirmed on the Wordpress Support Forums that posts_per_page only works if the type is set to scroll and not click, see http://wordpress.org/support/topic/posts_per_page-not-having-any-effect?replies=5#post-5058675

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.