Archived topic
Show related posts before "GP Social Share" in Blog posts only
5 replies · Started by boaz on August 4, 2021
Hi
following some question in this forum, im adding a "related posts" snippet to posts using the code shown here:
https://www.hongkiat.com/blog/wordpress-related-posts-without-plugins/
add_action( 'generate_after_main_content','show_related_posts', 5 );
function show_related_posts() { ?>
<div class="relatedposts">
<h3>Related posts</h3>
<?php
$orig_post = $post;
global $post;
$tags = wp_get_post_tags($post->ID);
if ($tags) {
$tag_ids = array();
foreach($tags as $individual_tag) $tag_ids[] = $individual_tag->term_id;
$args=array(
'tag__in' => $tag_ids,
'post__not_in' => array($post->ID),
'posts_per_page'=>4, // Number of related posts to display.
'caller_get_posts'=>1
);
$my_query = new wp_query( $args );
while( $my_query->have_posts() ) {
$my_query->the_post();
?>
<div class="relatedthumb">
<a rel="nofollow" target="_blank" href="<? the_permalink()?>"><?php the_post_thumbnail(array(150,100)); ?><br />
<?php the_title(); ?>
</a>
</div>
<? }
}
$post = $orig_post;
wp_reset_query();
?>
</div>
<?php }
1. Its currently showing last in the <footer class="entry-meta"></div> wrapper,
How can i make it show first ? (tried priority 5, its not helping), should i use a different hook ?
2. what hook should i use to show it on posts only (not on pages) ?
Thanks
Hi there,
keep the priority of 5 but change the hook from: generate_after_main_content to generate_after_content
This visual guide shows where the hooks are in the template ( the most meta uses the after_content hook )
https://docs.generatepress.com/article/hooks-visual-guide/#single-post
Hi David, i used the generate_after_content hook,
its still showing after the <footer class="entry-meta">
element
i want it to show before it. (attached image in private information)
2. you didnt answer my second questions - is there a way to show it only for posts or should i do this in the function logic ?
oops ... mean this hook:
generate_after_entry_content
And for it to display on single posts only you would need to include the condition in the function.
Or alternatively you can just register the function.
And the use a GP Hook Element to output it.
Works !
Thanks David
Glad to be of help