get_adjacent_post: Strange Behavior!
-
I’m putting together an archive of posts in a custom Post Type that are arranged sequentially both by Title and ID. My plan was to use get_adjacent_post to link to previous and next posts for a smooth browsing experience, but in practice it’s producing wildly unexpected results and I can’t figure out why.
Here’s an example: “0099” has an ID of 104, while “0098” has an ID of 103 and “0100” has an ID of 105. But in this case, $previous is generating a link to “0072” (ID 77) and $next is generating an error (visible in Debug Mode), as it does on every post.
Finally, here’s the preliminary template code:
<?php get_header(); ?> <!-- single-phase-one.php --> <main> <?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?> <article> <h1><?php the_title(); ?></h1> <?php the_content(); ?> <img style="width: 100%;" src="<?php bloginfo('url') ?>/comics/westward_<?php the_title(); ?>.jpg" /> <?php $previous = get_adjacent_post(); ?> <a href="<?php bloginfo('wpurl'); echo '/phase-one-comics/'; echo $previous->post_title; ?>">← <?php echo $previous->post_title; ?> </a> <?php $next = get_adjacent_post(false,'',true,''); ?> <a href="<?php bloginfo('wpurl'); echo '/phase-one-comics/'; echo $next->post_title; ?>"><?php echo $next->post_title; ?> →</a> </article> <?php endwhile; else: ?> <p><?php _e('Sorry, no posts matched your criteria.'); ?></p> <?php endif; ?> </main> <?php get_footer(); ?>
Thanks!
- The topic ‘get_adjacent_post: Strange Behavior!’ is closed to new replies.