Archived topic
Where to paste the code for related posts
5 replies · Started by Sanu Kumar on September 26, 2020
Hey there
I found PHP code for related posts but I am afraid where to paste because it can break my whole site so could you take a look at this and helps me to paste the code given below
<?php
$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),
'showposts'=>4, // Number of related posts that will be shown.
'caller_get_posts'=>1
);
$my_query = new wp_query($args);
if( $my_query->have_posts() ) {
echo '<div id="relatedposts"><h3>Related Posts</h3><ul>';
while ($my_query->have_posts()) {
$my_query->the_post();
?>
<?php
if ( has_post_thumbnail() ) { ?>
<li><div class="relatedthumb"><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_post_thumbnail(); ?><?php the_title(); ?></a></div></li>
<?php } else { ?>
<li><div class="relatedthumb"><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><img src="<?php echo get_post_meta($post->ID, 'Image',true) ?>" width="196" height="110" alt="<?php the_title_attribute(); ?>" /><?php the_title(); ?></a></div></li>
<?php }
?>
<?php
}
echo '</ul>';
}
}
$post = $backup;
wp_reset_query();
?>
I want to show related posts just after the content
Hi there,
We can't gaurantee that the code will work and won't cause any issues as it's not coming from us.
You can try to insert it using the generate_after_content hook using a hook element:
https://docs.generatepress.com/article/hooks-visual-guide/#single-post
https://docs.generatepress.com/article/hooks-element-overview/
Do you have a related post code snippet @leo ?
Hi there,
no we do not have a specific code for Related Posts.
You would be better off looking for a plugin that provides the specific kind of relationship you require.
Hey there
Can I know - Is this website is using "WP Show Post" plugin to show their "Related Post". If not, could you tell me the name of that plugin or it is custom coded?
It looks like it is custom developed.