-
Anonymous
Hello
How can I add a homepage or category loop to a single post page? I want to display the posts that are on homepage or any category bellow comments on a single post.
I tired that but it didn’t work.
function display_posts() { $args = array( 'post_type' => 'post', 'post_status' => 'publish', 'posts_per_page' => 10, ); $query = new WP_Query($args); if ($query->have_posts()) { while ($query->have_posts()) { $query->the_post(); get_template_part('content', get_post_format()); } } else { echo '<p>No posts found</p>'; } wp_reset_postdata(); }
here is the page that I’m working on: https://www.staging2.memescout.com/2294/
here are the screenshots:
-
Hi there,
that snippet creates the function called:
display_posts()
You need to make the callback.
Below the function add:display_posts();
-
Anonymous
now it works but the “Leave a comment” link is missing , it should of showed up like it does on homepage or any archive page.
-
Fernando
Hi there,
Can you try adding this through Appearance > Customize > Additional CSS?:
.single-post h3#reply-title { margin-bottom: 30px; }
-
Anonymous
i did that and it doesn’t work..
-
Fernando
It’s showing from my end now: https://share.zight.com/YEuzwGKv
Can you try clearing browser cache?
-
Anonymous
What i meant is that the link where it should say “Leave a Comment” is missing not the comment box…
missing link on post page:
https://freeimage.host/i/JCXFsNSbut on the homepage it appears:
https://freeimage.host/i/JCXKTbI
you can check it out by visiting: https://www.staging2.memescout.com/
-
Fernando
Can you try adding this snippet?:
add_filter( 'generate_header_entry_meta_items', function( $items ) { $items[] = 'comments-link'; return $items; } ); add_action( 'wp', function() { if ( is_single() ) { add_filter( 'generate_show_comments', '__return_true' ); } } );
Adding PHP: https://docs.generatepress.com/article/adding-php/#code-snippets
-
Anonymous
like this??
<?php function display_posts() { $args = array( 'post_type' => 'post', ); $query = new WP_Query($args); if ($query->have_posts()) { while ($query->have_posts()) { $query->the_post(); get_template_part('content', get_post_format()); } } else { echo '<p>No posts found</p>'; } wp_reset_postdata(); } add_filter( 'generate_header_entry_meta_items', function( $items ) { $items[] = 'comments-link'; return $items; } ); add_action( 'wp', function() { if ( is_single() ) { add_filter( 'generate_show_comments', '__return_true' ); } } ); display_posts(); ?>
-
Fernando
No, add that through functions.php if you’re using the Child theme or through a plugin like Code Snippets.
How are you adding this
display_posts()
function currently? -
Anonymous
-
Fernando
Try adding the code I shared through a plugin called Code snippets: https://wordpress.org/plugins/code-snippets/
-
Anonymous
I have that plugin installed also, but it won’t work because I can’t pick the location of the snippets like I do with the hooks. With hooks, I can select hook location, and it will put the code where I want it…which is right after the comments .. (generate_after_main_content)
-
Fernando
That code I shared is already a hook in itself. Placing it inside a Hook Element (which is also a hook) may cause issues.
It’s best practice to put codes like the codes I shared through Code Snippets of functions.php on a Child theme.
-
Anonymous
I have added it as a snippet, and it still doesn’t work.
-
Fernando
I see. Can you update the code to this instead?:
add_filter( 'generate_footer_entry_meta_items', function() { return array( 'date', 'categories', 'comments-link', 'post-navigation', ); } ); add_action( 'wp', function() { if ( is_single() ) { add_filter( 'generate_show_comments', '__return_true' ); } } );
- You must be logged in to reply to this topic.