[resolved] in sidebar, show other posts of same category (4 posts)

  1. tonywhite
    Posted 7 years ago #

    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?

  2. MichaelH
    Posted 7 years ago #

    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())
  3. tonywhite
    Posted 7 years ago #

    perfect. gracias!

  4. dallasbpeters
    Posted 6 years ago #

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

Topic Closed

This topic has been closed to new replies.

About this Topic