Skip to content

Instantly share code, notes, and snippets.

@vladimir-s-snippets
Created February 8, 2013 16:47
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vladimir-s-snippets/4740254 to your computer and use it in GitHub Desktop.
Save vladimir-s-snippets/4740254 to your computer and use it in GitHub Desktop.
<?php
$categories = get_the_category($post->ID);
if ($categories) {
$category_ids = array();
foreach($categories as $individual_category) $category_ids[] = $individual_category->term_id;
$args=array(
'category__in' => $category_ids,
'post__not_in' => array($post->ID),
'showposts'=>-1, // Number of related posts that will be shown.
'fields' => 'ids',
'caller_get_posts'=>1
);
$posts_ids = new wp_query($args);
$rnd_ids = array_slice(array_rand($posts_ids), 0 , 3);
// Rest is the same as the previous code
$my_query = new wp_query(array(
'post__in'=>$rnd_ids
));
if( $my_query->have_posts() ) {
echo '<b>Related Posts:</b><p>';
while ($my_query->have_posts()) {
$my_query->the_post();
?>
<a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title(); ?>"><?php the_title(); ?></a><br />
<?php
}
echo '</p>';
}
wp_reset_query();
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment