Site logo

Archived topic

How to change post count on blog home page vs others

4 replies · Started by Scott on December 18, 2017

Viewing posts 1–5 of 5

Hi

I would like the differ the count of posts shown on the home page of my blog vs other page i.e on the home page ( navigation page 1 ) i would like 3 posts shown but when i navigate to page 1,2..99 via the pagination i want there to be 4 posts show. I tried this with hooks but so far it's failing.

add_filter('pre_get_posts', 'posts_on_page', 1);

function posts_on_page($query){
    if ( $query->is_home() && $query->is_main_query() && ! is_admin() ) {
      $query->set( 'posts_per_page', 3 );
    }else{
      $query->set( 'posts_per_page', 4 );
    }
}

It continues to show the post per page configured in "Settings->Reading->Blog pages show at most"

Can someone help please ?

thanks
Scott

ok thanks but it still doesn't work for me. I tried this in the 'Inside Content Container' hook


<?php

function posts_on_page( $query ) {
    if ( ! is_main_query() ) {
        return;
    }

    if ( ! $query->is_paged ) {
        $query->set( 'posts_per_page', 10 );
    }
}

add_action( 'pre_get_posts', 'posts_on_page', 1 );

?>

The post count remains the same Settings > Reading

I used the snippets plugin instead of the hooks and it worked better. will resolve thanks.

Awesome :)

This archived topic is closed to new replies.