Show Different Posts Per Page/category in WordPress Website

by Comments closed In: Wordpress

Show different posts per page depending on context (e.g., homepage, search, archive)

Many times you need to show the different numbers of posts per category page in WordPress. Use the below code snippet to do that. Read about how to change URL prefix for custom post types in WordPress.

Show Different Posts

Code For Different Posts Per Page.

Show Different Posts Per Page

Add this code in function.php of your theme

Show Different Posts add in theme

function five_posts_on_homepage( $query ) {
    if ( $query->is_home() && $query->is_main_query() ) {
        $query->set( 'posts_per_page', 5 );
    }
}
add_action( 'pre_get_posts', 'five_posts_on_homepage' );

Categories