Want a get_posts list ordered by the current post first
-
On Single post page, I am showing a list of all posts within the current category using get_posts(). However, I want the list to start with the current post and proceed listing all other posts from this point. That way, as you navigate from post to post using the list, the current post will be indicated as “current” and will be on top of the rest- the order of posts will always remain the same, just starting from different positions.
I am “close-ish” in that I can highlight the current post as current, but I can’t begin the list from that point. Alternatively, I can exclude the current post from the loop, and include it prior to the loop using get_the_id() then position it at the top of the list. The problem with this approach is that the order of the list is not affected – it just reloads with same position on every page, just with the current post on top.
From what I gather, this can’t be done with the approach I’m using, which if correct seems absurd! BUT, perhaps another way?? thank you!
I’m just working locally for now, but will post site if helpful.
<?php $categories = get_the_category(); if ($categories) { $catID = $categories[0]->cat_ID; $catName = $categories[0]->name; } $postID = get_the_ID(); ?> <article class="single_post"> <div class="post_content"> <h2><?php the_title(); ?></h2> <?php the_content(); ?> </div><!-- .post_content --> <div class="thumbnail_column"> <h1><span><?php echo $catName; ?></span></h1> <ul class="thumb_vslider"> <li class="current_post"><?php if ( has_post_thumbnail() ) the_post_thumbnail('thumbnail'); ?></li> <?php $postslist = get_posts('posts_per_page=-1&order=DESC&exclude=' . $postID . '&category=' . $catID); foreach ($postslist as $post) : setup_postdata($post); ?> <li<?php if ( $postID == $post->ID ) echo ' class="current_post"'; ?>><a href="<?php the_permalink(); ?>"><?php if ( has_post_thumbnail() ) the_post_thumbnail('thumbnail'); ?></a></li> <?php endforeach;?> </ul> </div><!-- .thumbnail_column --> </article><!-- .single_post -->
- The topic ‘Want a get_posts list ordered by the current post first’ is closed to new replies.