BuddyBoss Home – Web Support Forums Themes Boss. theme How do I show only posts from a specific category in the post page?

Tagged: 

Viewing 7 posts - 1 through 7 (of 7 total)
  • Question

    #48574
    @patricia

    Hi Guys,

    I have bought your theme. Now I have a question. I have set a ‘Post Page’ (the page that all posts run in a loop) and I want to have the posts from a certain group of categories displayed in that page. Do you know how I can do that?

    For example, I have 2 categories: ‘food’ and ‘wine’. I want to show only the posts with the category ‘wine’ in the post page. How can I do that?

    Answers

    #48582

    Anonymous
    @

    Hi @patricia
    You can use coding approach with modifying loop codes

    http://codex.wordpress.org/The_Loop#Exclude_Posts_From_Some_Category

    You can also try https://wordpress.org/plugins/wp-posts-filter/

    Regards
    Varun Dubey

    #49049
    @patricia

    Hi @vapvarun

    If I want to go for the route of using coding approach. What do I need to do? This is because the plugin you suggested is very old and I think it might crash my theme. As I am not techy, could you tell my step by step about the procedure please? For example, 1) go to the file named blah blah blah, 2) rewrite the code in this file I’m sure there are lots of users who want to know how to do it too. And if you give a clear instruction, the user can read it and follow and there’s no need to ask you again.

    Regards,

    Patricia

    #49118

    Anonymous
    @

    Hi @patricia

    Try adding following code to child theme functions.php file

    
    add_action('pre_get_posts', 'bb_filter_categories');
    function bb_filter_categories($query) {
        if ($query->is_main_query() && is_page( 42 )) {
            $query->set('category_name','news, uncategorized');
        }
    }
    

    // When Page 42 (ID) is being displayed, you will need to replace 43 with blog page id.
    news, uncategorized replace them with a comma separated list of the categories you wish to include.
    Regards
    Varun Dubey

    #53107
    @patricia

    Hi @vapvarun

    Thank you for your reply. But it doesn’t really work. this is the code I put

     add_action('pre_get_posts', 'bb_filter_categories');
    	function bb_filter_categories($query) {
        if ($query->is_main_query() && is_page( 167 )) {
            $query->set('category_name','Plastic Surgery Doctors, uncategorized');
        }
    }

    So the questions are

    1) When I put the category name, Should it be the name or the slug?

    2) I have put this code in child theme functions.php file, do I need to put <?php ?> before and after the code you gave me?

    3) Is there anything wrong with my code?

    4) May I use category ID instead?

    Thank you!

    Regards,

    Patricia

    #53110
    @patricia

    Hi @vapvarun

    Finally, I have figured it out how to do it! This is the code that I’m using.

     add_action('pre_get_posts', 'bb_filter_categories');
    	function bb_filter_categories($query) {
        if ($query->is_main_query() && is_home) {
            $query->set('category__not_in', '10'); 
        }
    }

    This is because the ‘page id’ should be set as ‘is_home’ because it’s the post page. And I can choose the category that I want to exclude.

    So thank you for your patience! You’re fantastic! Thank you for your great theme!

    Many thanks!

    Patricia

    #53126

    Anonymous
    @

    🙂 Great

Viewing 7 posts - 1 through 7 (of 7 total)
  • The question ‘How do I show only posts from a specific category in the post page?’ is closed to new replies.