jump to navigation

How to Make Related Articles by Tags (without plugins) January 14, 2010

Posted by admin in : Website , trackback

One that is often used on every page of the article or post is the article relating to the previous articles so that visitors can easily read back and find the articles of your blog is still related to the topic or theme of your posts which did have the same article content.

Here is how to create a script Related Post by Tags on WordPress. This Related Post is not a plugin and need to do is put this script on singel.php.

<?php
//for use in the loop, list 5 post titles related to first tag on current post
$backup = $post; // backup the current object
$tags = wp_get_post_tags($post->ID);
echo “<div><h3>Related Posts</h3>”;
$tagIDs = array();
if ($tags)
{
$tagcount = count($tags);
for ($i = 0; $i < $tagcount; $i++) {
$tagIDs[$i] = $tags[$i]->term_id;
}
$args=array(
‘tag__in’ => $tagIDs,
‘post__not_in’ => array($post->ID),
’showposts’=>5,
‘caller_get_posts’=>1
);
$my_query = new WP_Query($args);
if( $my_query->have_posts() )
{
echo “<ul>”;
while ($my_query->have_posts()) : $my_query->the_post(); ?><li><a href=”<?php the_permalink() ?>” rel=”bookmark” title=”<?php the_title(); ?>”><?php the_title(); ?></a></li>
<?php endwhile;
echo “</ul>”;
}
} else echo “<span>No related posts were found!</span>”;
$post = $backup; // copy it back
wp_reset_query(); // to use the original query again
echo “</div>”;
?>

If you enjoyed this post, make sure you subscribe to my RSS feed!

Comments»

no comments yet - be the first?



Recent Search:
  how to make related article (1)