WordPress.org

Ready to get started?Download WordPress

Forums

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

  1. tonywhite
    Member
    Posted 5 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
    Member
    Posted 5 years ago #

    I believe some related posts plugins will do that, but try this in your sidebar.php:

    <?php
    if ( is_single() ) {
      $cats = wp_get_post_categories($post->ID);
      if ($cats) {
        $first_cat = $cats[0];
        $args=array(
          'cat' => $first_cat,
          'post__not_in' => array($post->ID),
          'showposts'=>5,
          'caller_get_posts'=>1
        );
        $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>
           <?php
          endwhile;
        } //if ($my_query)
      } //if ($cats)
      wp_reset_query();  // Restore global post data stomped by the_post().
    } //if (is_single())
    ?>
  3. tonywhite
    Member
    Posted 5 years ago #

    perfect. gracias!

  4. dallasbpeters
    Member
    Posted 4 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

Tags