Advertisement

Code to Create a related Posts without Any Plugins

 (Read 2978 times)

Admin

  • Administrator
  • Hero Member
  • *****
  • Posts: 1549
    • The Jucktion
Code to Create a related Posts without Any Plugins
« on: Dec 10, 2010, 10:08 AM »
Displaying related posts is a very great way to get visitors to stay longer on your blog. You could use a plugin, but to make things even easier you could use this snippet:

Code: [Select]
<?php
$tags 
wp_get_post_tags($post->ID);
if (
$tags) {
$tag_ids = array();
foreach($tags as $individual_tag$tag_ids[] = $individual_tag->term_id;

$args=array(
&#39;tag__in&#39; => $tag_ids,
&#39;post__not_in&#39; => array($post->ID),
&#39;showposts&#39;=>5, // Number of related posts that will be shown.
&#39;caller_get_posts&#39;=>1
);
$my_query = new wp_query($args);
if( $my_query->have_posts() ) {
echo &#39;<h3>Related Posts</h3><ul>&#39;;
while ($my_query->have_posts()) {
$my_query->the_post();
?>

<li><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></li>
<?php
}
echo &#39;</ul>&#39;;
}
}
?>


Enjoy

Advertisement

Best Affiliate Program