WordPress.org

Ready to get started?Download WordPress

Forums

Displaying "Related Tags" How to? Is there any plugins? (2 posts)

  1. gariben
    Member
    Posted 5 years ago #

    I want to display "Related Tags" on my article based on the tags I used for the article. Is there any plugins for this? I search but couldn't find any.

    I know there are alot of plugins to display "Related Posts".

    Thanks,
    Mke

  2. Ian.b
    Member
    Posted 5 years ago #

    Wp Recipes has a cod snippit that shows related post via tag. The original code on the page has an issue that it does close the wp_query and it will mess up your comments, if they fall after.

    The original code can be found here:
    http://www.wprecipes.com/how-to-show-related-posts-without-a-plugin/

    Below is my modified version that fixes that wp_query problem using wp_reset_query();

    <?php
      //for use in the loop, list 5 post titles related to first tag on current post
      $tags = wp_get_post_tags($post->ID);
      if ($tags) {
    
        $first_tag = $tags[0]->term_id;
        $args=array(
          'tag__in' => array($first_tag),
          'post__not_in' => array($post->ID),
          'showposts'=>5,
          'caller_get_posts'=>1
         );
            $my_query = new WP_Query($args);
    
            if( $my_query->have_posts() ) {
                echo '<div class="hr_bottom"></div><div id="relatedContent"><ul><h2>Related Posts</h2>';
    
              while ($my_query->have_posts()) : $my_query->the_post(); ?>
                <li><span class="title"><a href="<?php the_permalink() ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></span> <span class="meta"> - <?php the_time('l, F j, Y G:i'); ?> - <a href="<?php the_permalink() ?>#commenting" title="Jump to the comments"><?php comments_number('0 Comments','1 Comment','% Comments'); ?></a></span></li>
    
                 <?php
            endwhile;
        wp_reset_query();
          }
      echo '</ul></div>';
    }
    ?>

Topic Closed

This topic has been closed to new replies.

About this Topic