🚀 Is building a website too difficult? Let me guide you step by step—Learn about the 「Naibabiji WordPress Website Building Coaching Service」 →

3 Methods to Exclude Posts from Specific Categories on the WordPress Homepage

For the vast majority of websites usingWordPress Installation, the homepage displays all recently updated posts. For certain reasons, we may not want posts from all categories. You can achieve this through the following two methods:Exclude Specific Categories from Homepage

Plugin to Exclude Specific Categories from Homepage

Ultimate Category Excluder is aWordPress Pluginthat excludes post categories based on settings. It not only supports excluding posts from specific categories from the homepage but can also be set to exclude specific categories from TAGs, search results, archive pages, and RSS. It is very practical and suitable for novice users.Ultimate Category ExcluderUltimate Category Excluder is a free WP Plugin. You can download and install it from the link below, or directly search and install it from the Admin Dashboard.Download Link

Exclude Categories Using Code

If you don't want to install a plugin just to exclude category posts, you can also use code to achieve this. The specific method is to copy the following code into yourTheme's functions file.
function exclude_category_home( $query ) {
if ( $query->is_home ) {
$query->set( 'cat', '-5' );
}
return $query;
}

add_filter( 'pre_get_posts', 'exclude_category_home' );
In the above code, the second line determines the page: is_home is the homepage, is_search is the search results page. If you want other pages, you can also modify the code. The '-5' in the third line represents blocking all posts from the category with ID 5. You can extract the category ID by hovering your mouse over the category link in the URL. To exclude multiple categories, separate them with commas, e.g., '-5, -1'.If the homepage is a static page, how to exclude category posts on the blog archive page?Naibabiji currently uses a static page as the homepage, with blog posts called on another page. In this case, using the above code would cause both the homepage and the blog page not to display content from the excluded category. To exclude only from the homepage and not from the blog archive page, edit your website's Theme file (mine is index.php) and insert the following code.
The above code may need to be adapted. The query part isquery_posts($query_string .'&cat=-1');

🚀 Still feeling confused after reading the tutorial? Let me guide you step-by-step instead.

「Naibabiji WordPress Website Building Coaching」 — From selecting a domain and purchasing hosting to installing themes and publishing posts, I「ll guide you through every step, helping you avoid detours and reach your goals directly.

👉 Learn about Website Building Coaching Service
🔒

Comments are closed

The comment function for this article is closed. If you have any questions, please feel free to contact us through other channels.

×
二维码

Scan to Follow