Forums

[closed] TIP: Display all posts other than the current post (3 posts)

  1. Pete
    Member
    Posted 1 year ago #

    This handy little piece of code displays all the posts in a category other than the current post. Use this inside the loop.

    Hope it helps

    <?php
    if ( is_single()) {
      $categories = get_the_category();
      if ($categories) {
        foreach ($categories as $category) {
          // echo "<pre>"; print_r($category); echo "</pre>";
          $cat = $category->cat_ID;
          $args=array(
            'cat' => $cat,
    		'order' =>ASC,
            'post__not_in' => array($post->ID),
            'posts_per_page'=>55,
            'caller_get_posts'=>1
          );
          $my_query = null;
          $my_query = new WP_Query($args);
          if( $my_query->have_posts() ) {
            //echo '<h2>Other posts in the category of '. $category->category_description . ' &raquo; </h2>';
            //echo '<p>They are...  '. $category->category_description . ' </p>';
            while ($my_query->have_posts()) : $my_query->the_post(); ?>
              		<li><a href="<?php the_permalink(); ?>"> <?php the_title(); ?></a></li>
             <?php
            endwhile;
          } //if ($my_query)
        } //foreach ($categories
      } //if ($categories)
      wp_reset_query();  // Restore global post data stomped by the_post().
    } //if (is_single())
    ?>
  2. Pete
    Member
    Posted 1 year ago #

    bumping it up to keep it fresh :)

  3. Lets' not and say we did.

Topic Closed

This topic has been closed to new replies.

About this Topic