WordPress.org

Ready to get started?Download WordPress

Forums

Display Other Posts From Current Category(ies) (6 posts)

  1. awvickers
    Member
    Posted 2 years ago #

    I have a post that is in multiple categories and I'd like to show a listing of other posts that are in the related categories. The code below would be a starting point

    <?php query_posts('cat=1&showposts=5'); ?>
     <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
      <li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a>
      <?php the_excerpt() ?>
      </li>
      <?php endwhile; endif; ?>

    I dont know.... something like this?

    <?php
     $categories = get_the_category($post->ID);
     query_posts('cat=$categories&showposts=5'); ?>
     <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
      <li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a>
       </li>
      <?php endwhile; endif; ?>
  2. alchymyth
    The Sweeper & Moderator
    Posted 2 years ago #

    possibly:

    <?php
     $categories = wp_get_post_categories($post->ID);
     query_posts(array('category__in' => $categories, 'post__not_in' => array($post->ID), 'posts_per_page' => 5)); ?>
     <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
      <li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a>
       </li>
      <?php endwhile; endif; ?>

    http://codex.wordpress.org/Function_Reference/wp_get_post_categories

    http://codex.wordpress.org/Class_Reference/WP_Query#Parameters

  3. awvickers
    Member
    Posted 2 years ago #

    That didnt seem to work, or displayed nothing.

  4. alchymyth
    The Sweeper & Moderator
    Posted 2 years ago #

    where do you use that code?

    in single.php?
    or in the sidebar?

    what is the output if you add <?php echo $post->ID; ?> before that code?

  5. awvickers
    Member
    Posted 2 years ago #

    I'm using this code in a custom post type/template... or a custom single.php. The scenario I have is that this is the detail listing for a vendor that is a part of categories:

    Doors, Gutters, Roofing, Siding, Windows

    Now id like to list all the vendors in those categories making an area on the page "Similar Vendors"

    When adding <?php echo $post->ID; ?> it does give back the ID of 63

  6. alchymyth
    The Sweeper & Moderator
    Posted 2 years ago #

    I'm using this code in a custom post type/template... or a custom single.php.

    you might need to add the post type into the query;

    http://codex.wordpress.org/Class_Reference/WP_Query#Type_.26_Status_Parameters

Topic Closed

This topic has been closed to new replies.

About this Topic