[Resolved] limit the category for blog post

Home Forums Support [Resolved] limit the category for blog post

Home Forums Support limit the category for blog post

Viewing 10 posts - 1 through 10 (of 10 total)
  • Author
    Posts
  • #1019222
    Kelvin

    Dear Sir/Madam,

    This is the first I use the Site Library “SlideOut” I have different post categories, I want to limit the blog archive page to show some of the categories, how can I do that? By the way, for the single post, I also want to have the navigate with same category post only. How can I do that?

    Best regards,

    Kelvin

    #1019432
    David
    Staff
    Customer Support

    Hi there,

    1. You would need to use this PHP snippet:

    function exclude_categories_home( $query ) {
    	if ( $query->is_home() && $query->is_main_query() ) {
    		$query->set( 'cat', '-1, -5, -10' );
    	}
    	return $query;
    }
    	 
    add_filter( 'pre_get_posts', 'exclude_categories_home' );

    The -1, -5, -10 are the Category ID’s you want to remove. They must be prefixed by the -

    2. Post Navigation set to within same category – try this snippet:

    add_action( 'after_setup_theme', 'tu_category_specific_post_navigation' );
    function tu_category_specific_post_navigation() {
        add_filter( 'generate_category_post_navigation', '__return_true' );
    }

    https://docs.generatepress.com/article/adding-php/

    #1019554
    Kelvin

    Dear David,

    Sorry, both don’t work on my site. I try put the $query->set() outside $query->is_home if block, all categories post show up.

    When I view a single post, another category post still shows up.

    Best regards,

    Kelvin.

    #1019755
    David
    Staff
    Customer Support

    How did you add the code?

    #1019766
    Kelvin

    Dear David,

    Sorry, I placed to the wrong php script.

    How can I include the category instead of excluding it?

    Best regards,

    Kelvin

    #1019793
    David
    Staff
    Customer Support

    Just remove the - ie. -5 means remove cat ID 5. Whereas 5 means include cat ID 5.

    #1019795
    Kelvin

    Dear David,

    If I change the $query->set( ‘cat’, ‘3, 4’ );

    I got some problem, please visit https://prnt.sc/pbcs2j

    #1019845
    David
    Staff
    Customer Support

    I made an edit to the original code above. Use that and change it to the categories you want to select as per the last reply.

    #1021158
    Kelvin

    Dear David,

    Problem is solved.

    Best regards,

    Kelvin

    #1021159
    David
    Staff
    Customer Support

    You’re welcome

Viewing 10 posts - 1 through 10 (of 10 total)
  • You must be logged in to reply to this topic.