Skip to content

Instantly share code, notes, and snippets.

@chasereeves
Created August 14, 2012 16:30
Show Gist options
  • Save chasereeves/3350644 to your computer and use it in GitHub Desktop.
Save chasereeves/3350644 to your computer and use it in GitHub Desktop.
Wordpress similar posts without plugin
<?php
//=========================================================== SIDEBAR == Similar Posts
function similar_posts() {
echo "<li class=\"widget similar_posts\">";
echo "<h3>Similar Posts</h3>";
echo "<ul>";
global $post;
//stores the original post data
$original_post = $post;
//get the post category
$category = get_the_category();
$cat = $category[0]->cat_ID;
//start loop and query stuff
$args = array(
'orderby' => 'rand',
'cat' => $cat,
'post__not_in' => array($post->ID),
'showposts'=> 8,
'caller_get_posts'=> 1
);
$related_posts_sidebar = new WP_Query($args);
if ( $related_posts_sidebar->have_posts() ) :
while ( $related_posts_sidebar->have_posts() ) : $related_posts_sidebar->the_post();
?>
<li><a title="<?php the_title(); ?>" href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
<?php
endwhile;
wp_reset_query();
endif;
echo "</li>";
echo "</ul>";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment