Show Different Posts Per Page/category in WordPress Website

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

Many times you need to show different number of post per category page in WordPress. Use below code snippet 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

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' );

Leave a Reply

Your email address will not be published. Required fields are marked *

Categories

Leave a Reply

Your email address will not be published. Required fields are marked *