- This topic has 11 replies, 3 voices, and was last updated 3 years, 6 months ago by Tom.
-
AuthorPosts
-
October 14, 2020 at 7:18 am #1488537Michal
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!
October 14, 2020 at 10:24 am #1488978LeoStaffCustomer SupportHi there,
Using a plugin like WP Show Posts should be the easiest:
https://wordpress.org/plugins/wp-show-posts/October 15, 2020 at 4:35 am #1490101MichalHi 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
MichalOctober 15, 2020 at 10:15 am #1490811TomLead DeveloperLead DeveloperHi 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?October 15, 2020 at 10:51 am #1490856MichalHi 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
MichalOctober 15, 2020 at 3:12 pm #1491132TomLead DeveloperLead DeveloperStrange, 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?October 16, 2020 at 12:28 am #1491481MichalHi 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" }
October 16, 2020 at 9:50 am #1492292TomLead DeveloperLead DeveloperThanks 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; } );
October 19, 2020 at 4:35 am #1495308MichalHi Tom,
after adding generate_entry_meta_post_types filter, author and date are correctly rendered, but categories, tags and comments links are still missing.
October 19, 2020 at 10:13 am #1495917TomLead DeveloperLead DeveloperAdded one more filter for that: https://generatepress.com/forums/topic/display-recent-posts-using-the-same-layout-as-in-archive/#post-1492292
October 20, 2020 at 1:35 am #1496573MichalHi 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
October 20, 2020 at 9:33 am #1497353TomLead DeveloperLead DeveloperIt’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 π
-
AuthorPosts
- You must be logged in to reply to this topic.