WordPress.org

Ready to get started?Download WordPress

Forums

Customizr
[resolved] How can we list posts from single category on posts page? (5 posts)

  1. Keith Ratner
    Member
    Posted 7 months ago #

    On the 'Posts Page' selected under 'Front Page' options, is there a snippet available to force that page to only display posts from a specific category, or even multiple categories?

  2. acub
    Member
    Posted 7 months ago #

    add_filter('pre_get_posts', 'limit_homepage_posts');
    function limit_homepage_posts($query) {
    	if ( $query->is_home() && $query->is_main_query() )
    		$query->set( 'cat', 'X,Y' );
    	}

    Replace X,Y with category ids. Or, if you want to use slugs, use

    $query->set( 'category_name', 'slug1,slug2' );

    This function (and the preceding filter) should go in functions.php of your child theme.

  3. Keith Ratner
    Member
    Posted 7 months ago #

    Perfect. Thanks!

  4. afizan
    Member
    Posted 7 months ago #

    Hi I am a bit confuse on how the filter function. Could you elaborate a little bit more please.

    Did I need to create a new page with the same nae of category?

  5. acub
    Member
    Posted 7 months ago #

    You don't need to create a template. The function above filters the posts displayed on homepage.

    More on how filters work in WP here. It's what makes WP flexible.

Reply

You must log in to post.

About this Theme

About this Topic

Tags