WordPress.org

Forums

Get next/previous post id in a particular loop (2 posts)

  1. Clicknathan
    Member
    Posted 3 years ago #

    I have a custom loop setup via WP_Query that calls the latest 3 posts from a custom post type called cpt_apps where each post also has a certain custom field associated. I'm trying to display the post thumbnail of the next and previous posts with each item in the loop.

    So for example, I want to show:

    Post #1 Title
    Post #1 PostThumbnail
    Post #3 PostThumbnail // prev post thumb
    Post #2 Post Thumbnail // next post thumb
    _______
    Post #2 Title
    Post #2 PostThumbnail
    Post #1 PostThumbnail // prev post thumb
    Post #3 Post Thumbnail // next post thumb
    _______
    Post #3 Title
    Post #3 PostThumbnail
    Post #2 PostThumbnail // prev post thumb
    Post #1 Post Thumbnail // next post thumb
    _______

    ...for each instance in the loop.

    I've tried get_previous_post and get_next_post, but I can't seem to limit them to the specific custom field, and also they're just showing the current post's thumbnail anyway.

    Would anyone have any idea as to how this could be done?

    Here's the code as I have it now:

    <?php $my_query = new WP_Query(array ( 'post_type' => 'cpt_apps', 'orderby' => 'meta_value', 'meta_key' => 'll_appfeatured', 'posts_per_page' => 3) );
    				  $postnum = 0;
    				  while ($my_query->have_posts()) : $my_query->the_post();
    			      $appprice = get_post_meta($post->ID, 'll_appprice', true);
    			      $appicon = get_post_meta($post->ID, 'll_appicon', true);?>
    
    				  <?php $prevPost = get_previous_post(false); $prevthumbnail = get_the_post_thumbnail($prevPost->ID, array(445, 322)); ?>
    				  <?php $nextPost = get_next_post(false); $nextthumbnail = get_the_post_thumbnail($nextPost->ID, array(445, 322)); ?>
    
    				<li><article id="post-<?php the_ID(); ?>" class="hentry featured-app-post">
    
    				<a href="<?php the_permalink(); ?>">
    					<?php the_post_thumbnail('apps-large'); ?>
    
    					<div class="backing">
    						<?php echo $prevthumbnail; ?>
    						<?php echo $nextthumbnail; ?>
    					</div>
    
    					<div class="hover-up">
    
    						<h1><?php if ($appicon != '') { ?><img src="<?php echo $appicon; ?>" class="app-icon" /><?php } ?><?php the_title(); ?></h1>
    						<img src="/wp-content/themes/dashburst/img/apps/app-average-review.png" /><br/>
    						<small class="p-excerpt"><?php the_excerpt_rss(); ?></small>
    						<a class="button apps-button" href="<?php the_permalink(); ?>">Read More &rarr;</a>
    
    					</div>
    
    				</a>
    
    				</article></li>
    
    				<?php endwhile; ?>
  2. paddelboot
    Member
    Posted 2 years ago #

    Try this:

    $next_post = $my_query->posts[ $my_query->current_post + 1 ];
    $prev_post = $my_query->posts[ $my_query->current_post - 1 ];

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.