Exclude categories from blog page "Pro" "Ethos"

Hi there,
I know this question has been answered a couple of times but I have tried every solution I saw and none works for me.

I am trying to exclude specific post categories ( Gallery and Projects ) from displaying among the blog posts page.

Here’s the code I’m using in the Pro child theme:

function x_exclude_cat_from_blog($query) {
if ($query->is_home() && $query->is_main_query()) {
$query->set(‘cat’, ’ -25, -26 ');
}
}
add_action(‘pre_get_posts’, ‘x_exclude_cat_from_blog’);

Blog page: https://pccned.davosban.com/blog/

Kindly assist, thanks.

Hi @itsmisterdavid,

Thank you for reaching out to us. To exclude certain categories from the blog page, you can add the following code in your child theme’s functions.php file:

function exclude_category( $query ) {
    if ( ( $query->is_home() || $query->is_archive() ) && $query->is_main_query() ) {
        $query->set( 'cat', '-25, -26' );
    }
}
add_action( 'pre_get_posts', 'exclude_category' );

Make sure the category IDs are correct, you can follow this article to find out the correct category IDs: https://www.wpbeginner.com/beginners-guide/how-to-find-post-category-tag-comments-or-user-id-in-wordpress/

Please note that the code provided above serves as a guide only and is to help you in getting started so implementing it and maintaining the code will be out of our support scope and for further maintenance for new possible versions of the theme that may cause the customization to break, you will need to hire a developer.

Hope this helps!

Thanks for your response @nabeel but, the code did not work.
The code is currently sitting in my child theme functions.php but the blog archive page still displays the gallery posts.
Please help again. I am including my login details in the secure note.

Hey @itsmisterdavid,

Please disregard the code as we do not provide support for custom codes. It’s only a guide. We cannot investigate why it doesn’t work.

Instead, please create a custom Archive Layout in Pro. The Layout Builder allows including or excluding categories.

image

Please learn from the following links how to use the Layout Builder.

Thanks.

This topic was automatically closed 10 days after the last reply. New replies are no longer allowed.