Improved code – Show related posts in WordPress without a plugin

A while ago I posted an article about showing related posts in WordPress without the use of a plugin. Recently I have found better code for this and I would like to share it with you <?php $tag_ids = wp_get_post_tags( $post->ID, array( ‘fields’ => ‘ids’ ) ); $args = array( ‘tag__in’ => $tag_ids, ‘post__not_in’ => …

Show related posts in WordPress without a plugin

There are plugins that provide you with the ability to show related posts below a post. But it’s possible to do this without using plugins. This code uses tags attached to posts to show 5 related posts in a list. <?php $tags = wp_get_post_tags($post->ID); if ($tags) { $tag_ids = array(); foreach($tags as $individual_tag) $tag_ids[] = …