[Support request] Display recent posts using the same layout as in archive

Home Forums Support [Support request] Display recent posts using the same layout as in archive

Home Forums Support Display recent posts using the same layout as in archive

  • This topic has 11 replies, 3 voices, and was last updated 1 year ago by Tom.
Viewing 12 posts - 1 through 12 (of 12 total)
  • Author
    Posts
  • #1488537
    Michal

    Hello, I have Gutenberg managed page set as website homepage. I want to display list of recent posts in a part of this page and I would like to have exactly the same post layout as Generatepress uses in archives or on blog page (with excerpts, not full contents). Standard block in Gutenberg uses different layout, so I tried to create custom short_code:

            add_shortcode('my_recent_posts', function() {
                $args = [
                    'post_type' => 'post',
                    'post_status' => 'publish',
                    'posts_per_page' => 10,
                ];
    
                $output = '';
                $the_query = new WP_Query($args);
                if ($the_query->have_posts()) {
                    ob_start();
                    while ($the_query->have_posts()) {
                        $the_query->the_post();
                        get_template_part('content');
                    }
                    $output = ob_get_contents();
                    ob_end_clean();
                }
                wp_reset_postdata();
    
                return $output;
            });
    

    But posts are rendered like on single post page – post title is wrapped in h1 instead of h2, it is without link, no date and author is displayed. I cannot find a way how to achieve what I need. Should I use different template in get_template_part? Thank you for any advice!

    #1488978
    Leo
    Staff
    Customer Support

    Hi there,

    Using a plugin like WP Show Posts should be the easiest:
    https://wordpress.org/plugins/wp-show-posts/

    #1490101
    Michal

    Hi Leo,

    thank you, I tried it. It is close, but not the same – there are some small differences like swapped publish date and author or missing taxonomy icons in WP Show Posts, no background color, etc. If there is no better solution, I could use filters and styles to resemble the general archive look closer, but if Generatepress changes the layout in the future, the output of WP Show Posts will not reflect it, and I will need to adapt it again. It would be easier for me to use the theme’s default template to keep the look and feel of posts on my home page the same as in the archive.

    Regards
    Michal

    #1490811
    Tom
    Lead Developer
    Lead Developer

    Hi there,

    get_template_part('content') should do the trick, but it’s likely that GP is detecting that it’s a page and not an archive, so those elements aren’t working.

    Does this change the title?:

    add_filter( 'generate_get_the_title_parameters', function( $params ) {
        if ( is_front_page() ) {
            $params = array(
                'before' => sprintf(
                    '<h2 class="entry-title"%2$s><a href="%1$s" rel="bookmark">',
                    esc_url( get_permalink() ),
                    'microdata' === generate_get_schema_type() ? ' itemprop="headline"' : ''
                ),
                'after' => '</a></h2>',
            );
        }
    
        return $params;
    } );

    The post meta thing is strange, as it should work as long as each post is a post post type – any chance you can link me to the page?

    #1490856
    Michal

    Hi Tom, thank you for the filter – yes, it fixes the title. Regarding the post meta, I am attaching website link and access information in private part of the message.

    Regards
    Michal

    #1491132
    Tom
    Lead Developer
    Lead Developer

    Strange, could be a couple of blockers as we’re telling GP to do something it’s not really aware of.

    If you find this line: https://github.com/tomusborne/generatepress/blob/3.0.2/inc/structure/post-meta.php#L512

    What happens if you add var_dump('hi'); above it? Do you see the “hi” text on your home page?

    #1491481
    Michal

    Hi Tom, var_dump inserted above the line you pointed on doesn’t execute. I var_dumped variables used in that if condition and it seems that problem is in post type:

    var_dump(get_post_type()):

    
    string(4) "page"
    

    var_dump($header_post_types):

    
    array(1) {
      [0]=>
      string(4) "post"
    }
    

    var_dump($header_items):

    
    array(2) {
      [0]=>
      string(4) "date"
      [1]=>
      string(6) "author"
    }
    
    #1492292
    Tom
    Lead Developer
    Lead Developer

    Thanks for that!

    Try this:

    add_filter( 'generate_entry_meta_post_types', function( $types ) {
        if ( is_front_page() ) {
            $types[] = 'page';
        }
    
        return $types;
    } );
    
    add_filter( 'generate_footer_meta_post_types', function( $types ) {
        if ( is_front_page() ) {
            $types[] = 'page';
        }
    
        return $types;
    } );
    #1495308
    Michal

    Hi Tom,

    after adding generate_entry_meta_post_types filter, author and date are correctly rendered, but categories, tags and comments links are still missing.

    #1495917
    Tom
    Lead Developer
    Lead Developer
    #1496573
    Michal

    Hi Tom,

    thank you very much for you support. Your last filter almost did it, just comments link was missing, but I found, that I need to add one more filter:

    
           add_filter('generate_show_comments', function($default_display) {
                if (is_front_page()) {
                    return true;
                }
                return $default_display;
            });
    

    Now it works perfectly. Maybe it could be an idea for future improvements to make this task easier πŸ™‚

    Thanks again!

    Michal

    #1497353
    Tom
    Lead Developer
    Lead Developer

    It’s a tough one as displaying the post loop on static pages isn’t something WordPress is really built to do. Cool idea though – I’m glad it worked. Will definitely see if there’s anything we can do to make it easier πŸ™‚

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