Site logo

Archived topic

How to show last loop posts in home page by one category and subcategories

5 replies · Started by Ignazio on September 25, 2020

Viewing posts 1–6 of 6

Good morning my dear friends, in the home page, I would like to insert the loop of the last articles in a certain category and hierarchical subcategories. Would you be so kind as to tell me how I can do it? I have Generatepress Pro.

In the home page loop, you currently see all articles from all categories. I would like to see only articles belonging to a specific category.

Hi there,

will there be a separate Blog page that shows all categories?

If so you we would recommend creating a static Page for the Home and use the WP Show Posts plugin to list the posts you want:

https://wordpress.org/plugins/wp-show-posts/

Hi David, thank you very much for your answer. Actually I was looking for a code that would allow me to display on the home page the loop of a specific category. I didn't want to create a page to be inserted in the home page to avoid formatting and aesthetics problems. In fact I wanted the site to have the same aesthetics for home page loops and other types of archives. As for the categories, there is no problem because it is enough to insert in the menu a link to the category, but in the home page, it is the theme to manage everything.

Try this PHP Snippet:

function single_category_homepage( $query ) {
if ( $query->is_home() && $query->is_main_query() ) {
$query->set( 'cat', '7');
}
}
add_action( 'pre_get_posts', 'single_category_homepage' );

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

Simply change the 7 to the ID of the Category you want displayed.

This archived topic is closed to new replies.