TIP: Display all posts other than the current post
-
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 . ' » </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()) ?>
Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
- The topic ‘TIP: Display all posts other than the current post’ is closed to new replies.