Created
February 8, 2013 16:47
-
-
Save vladimir-s-snippets/4740254 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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