Skip to content

Instantly share code, notes, and snippets.

@georgioupanayiotis
Created February 14, 2015 20:00
Show Gist options
  • Save georgioupanayiotis/8c058536ce6cd01eee34 to your computer and use it in GitHub Desktop.
Save georgioupanayiotis/8c058536ce6cd01eee34 to your computer and use it in GitHub Desktop.
Display related posts in WordPress
// 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="related_posts">
<h2>Related Tutorials</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();
// If comments are open or we have at least one comment, load up the comment template.
if ( comments_open() || get_comments_number() ) {
comments_template();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment