Support » Fixing WordPress » in sidebar, show other posts of same category

  • Resolved tonywhite


    I’m using wordpress 2.7.

    In the sidebar of a single post, I want to display a list of other posts of the same category as the one currently displayed. I want to exclude the currently displayed post title from that list.

    If no other additional posts in that category exists, do nothing.

    Anyone know how I can accomplish this?

Viewing 3 replies - 1 through 3 (of 3 total)
  • I believe some related posts plugins will do that, but try this in your sidebar.php:

    if ( is_single() ) {
      $cats = wp_get_post_categories($post->ID);
      if ($cats) {
        $first_cat = $cats[0];
          'cat' => $first_cat,
          'post__not_in' => array($post->ID),
        $my_query = new WP_Query($args);
        if( $my_query->have_posts() ) {
          echo 'Related Posts';
          while ($my_query->have_posts()) : $my_query->the_post(); ?>
            <p><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
        } //if ($my_query)
      } //if ($cats)
      wp_reset_query();  // Restore global post data stomped by the_post().
    } //if (is_single())

    perfect. gracias!

    what if I wanted ‘Related Posts’ to be dynamic based on the category title? help?

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘in sidebar, show other posts of same category’ is closed to new replies.