Display posts belonging to the categories of current post
-
Hello, I would like to display posts belonging to the categories of the current post without duplicating the present post. I found the following code on this forum and it works great. Now I’d like to add the excerpt and the date. What is the easiest way of going about this?
<?php global $post; $cat_ID=array(); $categories = get_the_category(); //get all categories for this post foreach($categories as $category) { array_push($cat_ID,$category->cat_ID); } $args = array( 'orderby' => 'date', 'order' => 'DESC', 'post_type' => 'post', 'numberposts' => 8, 'post__not_in' => array($post->ID), 'category__in' => $cat_ID ); // post__not_in will exclude the post we are displaying $cat_posts = get_posts($args); $out=''; foreach($cat_posts as $cat_post) { $out .= '<li>'; $out .= '<a href="'.get_permalink($cat_post->ID).'" title="'.wptexturize($cat_post->post_title).'">'.wptexturize($cat_post->post_title).'</a></li>'; } $out = '<ul class="cat_post">' . $out . '</ul>'; echo $out; ?>
Thanks in advance!
Viewing 8 replies - 1 through 8 (of 8 total)
Viewing 8 replies - 1 through 8 (of 8 total)
- The topic ‘Display posts belonging to the categories of current post’ is closed to new replies.