[Support request] How to show last loop posts in home page by one category and subcategories

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

Viewing 6 posts - 1 through 6 (of 6 total)
  • Author
    Posts
  • #1458699
    Ignazio

    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.

    #1458732
    Leo
    Staff
    Customer Support

    Hi there,

    Not sure if I fully understand.

    Is the home page the posts page or a static page?

    GP doesn’t handle the posts loop as it’s handled by WordPress itself:
    https://developer.wordpress.org/reference/classes/wp_query/

    #1468675
    Ignazio

    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.

    #1468866
    David
    Staff
    Customer Support

    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/

    #1470301
    Ignazio

    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.

    #1470406
    David
    Staff
    Customer Support

    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.

Viewing 6 posts - 1 through 6 (of 6 total)
  • You must be logged in to reply to this topic.