Skip to content

Instantly share code, notes, and snippets.

@webhasan
Created January 26, 2014 05:47
Show Gist options
  • Save webhasan/8629000 to your computer and use it in GitHub Desktop.
Save webhasan/8629000 to your computer and use it in GitHub Desktop.
wp: related post
<?php $orig_post = $post;
global $post;
$categories = get_the_category($post->ID);
if($categories){
$categori_id = array();
foreach ($categories as $category) {
$categori_id[] = $category->cat_ID;
}
$post_loop = new WP_Query(array(
'posts_per_page' => 3,
'category__in' => $categori_id,
'post__not_in' => array($post->ID),
'orderby' => 'rand'
));
if($post_loop->have_posts()){ ?>
<h2 class="related_post_title">Suggested Articles For You:</h2>
<div class="related-post-area">
<ul>
<?php while($post_loop->have_posts()){ ?>
<?php $post_loop->the_post(); ?>
<li>
<a href="<?php the_permalink(); ?>" class="thumbnail-image" ><?php the_post_thumbnail(); ?></a>
<h1 class="thumbnail-text"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1>
</li>
<?php } ?>
</ul>
</div>
<?php }
}
$post = $orig_post;
wp_reset_query();
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment