Last updated December 6th, 2023 00:12
If you manage a blog on WordPress, there may be times when you want to display only posts from a specific category on the blog’s homepage, excluding others. WordPress doesn’t have any native settings for this behavior. Of course, you could solve this with a plugin. However, if you want to save space and memory, you can easily achieve the same result with a snippet. Let’s take a look at snippet for category-specific post display on homepage.
Snippet for category-specific post display on homepage
First, let’s take a look at the specific snippet. Then we’ll briefly discuss it.
is_home() && $query->is_main_query()) {
$query->set('category_name', 'category_name');
}
}
add_action('pre_get_posts', 'show_specific_category_posts');
?>
This snippet modifies the query for loading posts on the homepage (is_home()) and ensures that only posts from a specific category are displayed.
Description of the individual code parts:
- Function show_specific_category_posts: This is a custom function that accepts a single parameter, $query, which is the query object used by WordPress to load posts.
- if ($query->is_home() && $query->is_main_query()): This condition checks if it is the homepage and if it is the main query being loaded. This ensures that this query modification is applied only to the homepage.
- Method $query->set(‘category_name’, ‘category_name’): This method is used to set the category_name parameter in the query. In this case, the parameter is set to the specific name of the category you want to display. Instead of ‘category_name’, you need to replace it with the actual name of the desired category. — If you want to use the category ID instead of the name, you can use the method $query->set(‘cat’, ‘category_ID’), where ‘category_ID’ is the numerical ID value of the desired category.
- Function add_action(‘pre_get_posts’, ‘show_specific_category_posts’): This function registers the pre_get_posts action and triggers the show_specific_category_posts function when invoked. The pre_get_posts action is called before executing the post query, allowing you to modify the query before loading the posts.
To make this snippet functional, you need to follow these steps:
- Replace ‘category_name‘ with the actual name of the category you want to display. You can also use the category ID by using the method $query->set(‘cat’, ‘category_ID’).
- Add this code to the functions.php file in your current WordPress theme. You can use the file editor in the WordPress administration or access the file via FTP.
By doing this, the homepage will load posts only from the specified category.
The website is created with care for the included information. I strive to provide high-quality and useful content that helps or inspires others. If you are satisfied with my work and would like to support me, you can do so through simple options.
Byl pro Vás tento článek užitečný?
Klikni na počet hvězd pro hlasování.
Průměrné hodnocení. 0 / 5. Počet hlasování: 0
Zatím nehodnoceno! Buďte první
Je mi líto, že pro Vás nebyl článek užitečný.
Jak mohu vylepšit článek?
Řekněte mi, jak jej mohu zlepšit.
Subscribe to the Newsletter
Stay informed! Join our newsletter subscription and be the first to receive the latest information directly to your email inbox. Follow updates, exclusive events, and inspiring content, all delivered straight to your email.