• I’m having problems getting next and previous post links to work on my single.php page.
    It’s a simple theme; There are 2 types of posts comics and blog posts. Here is my code:

    <?php get_header(); ?>
    	<div class="wrapper2">
    		<div class="container">
    		<div class="padding"></div>
    		<?php if (have_posts()) : while (have_posts()) : the_post(); ?>
    
    	<?php if (in_category(3)) { ?>
    		<div class="main_comic">
    			<img src="<?php the_field('the_comic'); ?>" alt="<?php the_title(); ?>" class="fit_img">
    		</div>
    		<div class="post_info">
    			<h1 class="post_title"><?php the_title(); ?></h1>
    			<div class="share_this"><?php echo sharing_display(); ?></div>
    
    			<div class="post_navigation">
    				<?php next_post_link('%link', 'Next post in category', TRUE); ?>
    			</div>
    
    		</div>
    	<?php } else { ?>
    		<article>
    			<h1><?php the_title(); ?></h1>
    			<h2><?php the_field('sub_header'); ?></h2>
    			<time datetime="<?php the_time('Y-m-d'); ?>"><?php the_date(); ?></time>
    			<?php the_content(); ?>
    		</article>
    		<div class="post_info">
    			<div class="share_this"><?php echo sharing_display(); ?></div>
    
    			<div class="post_navigation on_blog">
    				<?php next_post_link('%link', 'Next post in category', TRUE); ?>
    			</div>
    
    		</div><br>
    	<?php } endwhile; else: ?>
    	<?php endif; ?>
    		</div>
    	</div>
    <?php get_footer(); ?>

    Here is the website is Monstrouspulp.com.

    I’ve tried several ways to get this to work. I read somewhere that it might have to do with setting the post ID. I have tried replacing the next_post_link code above with this:

    <div class="post_navigation">
    				<?php
    					$post_id = $post->ID; // current post id
    					$cat = get_the_category();
    					$current_cat_id = $cat[0]->cat_ID; // current category Id 
    
    					$args = array('category'=>$current_cat_id,'orderby'=>'post_date','order'=> 'DESC');
    					$posts = get_posts($args);
    					// get ids of posts retrieved from get_posts
    					$ids = array();
    					foreach ($posts as $thepost) {
    					    $ids[] = $thepost->ID;
    					}
    					// get and echo previous and next post in the same category
    					$thisindex = array_search($post->ID, $ids);
    					$previd = $ids[$thisindex-1];
    					$nextid = $ids[$thisindex+1];
    
    					if (!empty($previd)){
    				?>
    					<a rel="prev" href="<?php echo get_permalink($previd) ?>"><div class="arrow-left genericon genericon-leftarrow"></div></a>
    
    				<?php }	if (!empty($nextid)){ ?>
    					<a rel="next" href="<?php echo get_permalink($nextid) ?>"><div class="arrow-right genericon genericon-rightarrow"></div></a>
    				<?php } ?>
    			</div>

    This works perfectly for the 5 most recent posts then it stops and you can’t get to the rest.
    So, what is my problem? I’m leaving the site up with the second set of code in it so it at least somewhat works. If anyone wants to see how it works with the first set of code let me know.
    Thanks.

  • The topic ‘next_post_link always gives me the first post’ is closed to new replies.