Modify popular_posts_per_category function
-
I found the following code for adding links to posts in the same category. It works great, but I need a modification, and I don’t know how to do it.
function popular_posts_per_category() { global $post; $categories = get_the_category(); foreach($categories as $category) { $cats[] = $category->cat_ID; } $cats_unique = array_unique($cats); $args = array( 'category__in' => $cats_unique, 'orderby' => 'date', 'order' => 'DESC', 'post_type' => 'post', 'post_status' => 'publish', 'posts_per_page' => 50 ); echo '<ul>'; $popular_posts = null; $popular_posts = new WP_Query($args); while ($popular_posts->have_posts()) : $popular_posts->the_post(); $title_trim = get_the_title(); if (strlen($title_trim) > 60) { $title_trim = substr($title_trim,0,60); } ?> <li><a href="<?php the_permalink(); ?>"><?php echo $title_trim; ?></a></li> <?php endwhile; rewind_posts(); echo '</ul>'; }
Basically, this function returns links to posts in descending date order, newest post first. I need to modify it so that, instead of starting with the most recent post in the category, it starts with the current post as most recent and works its way back from there.
So if the current post was published on June 14, 2014, and the newest post in the category was published July 26, 2014, instead of showing the 50 most recent posts in the category starting July 26, 2014, I want it to show posts starting with the post most immediately preceding this current post, published before June 14, 2014.
Can anyone tell me how to modify this function in order to accomplish this change?
- The topic ‘Modify popular_posts_per_category function’ is closed to new replies.