Showing posts with label Display WordPress Related Posts. Show all posts
Showing posts with label Display WordPress Related Posts. Show all posts

Monday 3 November 2014

How To Display Related Posts In WordPress


How To Display Related Posts In WordPress

In this post I will show you how to display related posts in WordPress. Open your theme’s single.php file and add following code just above the comments code like below :

// related posts
$related = get_posts( array( 'category__in' => wp_get_post_categories($post->ID), 'numberposts' => 6, 'post__not_in' => array($post->ID) ) );

if( $related ){ ?>
<div class="posts">
<h2>Related Posts</h2>
<ul>
<?php foreach( $related as $post ) {
setup_postdata($post); ?>
    <li>
        <div class="related_post">
        <?php if ( has_post_thumbnail() ) : ?>
            <a title="<?php the_title(); ?>" href="<?php the_permalink(); ?>"><?php the_post_thumbnail(array(150,150) ); ?></a>
        <?php endif; ?>

        <a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a>
        </div>
    </li>
<?php } ?>
</ul>
</div>
<?php }
wp_reset_postdata();
?>