theme development - Previous and Next links not populating, have I missed something?

I'm working on a very simple theme. I have tried adding next and previous links and cannot get them to work.After t

I'm working on a very simple theme. I have tried adding next and previous links and cannot get them to work.

After trying a few different things, I ended up with this index.php (mostly based on answers on SO).

<?php get_header(); ?>
<!-- html stuff --> 
    <?php 
    if ( have_posts() ) { 
       while ( have_posts() ) : the_post();
           // ... post layout stuff
       endwhile;
    } 
    ?>
<!-- Other html bits -->
<?php           
   $prev_link = get_previous_posts_link(__('&laquo; Older Entries'));
   $next_link = get_next_posts_link(__('Newer Entries &raquo;'));
   if ($prev_link || $next_link) {
       echo '<nav><ul class="pager">';
       if ($prev_link){
           echo '<li>'.$prev_link .'</li>';
       }
       if ($next_link){
           echo '<li>'.$next_link .'</li>';
       }
       echo '</ul></nav>';
   }
?>
<!-- html stuff --> 
<?php get_footer(); ?>

There are two blog posts on the development install I am working with. But neither of them display with the navigational links.

I'm working on a very simple theme. I have tried adding next and previous links and cannot get them to work.

After trying a few different things, I ended up with this index.php (mostly based on answers on SO).

<?php get_header(); ?>
<!-- html stuff --> 
    <?php 
    if ( have_posts() ) { 
       while ( have_posts() ) : the_post();
           // ... post layout stuff
       endwhile;
    } 
    ?>
<!-- Other html bits -->
<?php           
   $prev_link = get_previous_posts_link(__('&laquo; Older Entries'));
   $next_link = get_next_posts_link(__('Newer Entries &raquo;'));
   if ($prev_link || $next_link) {
       echo '<nav><ul class="pager">';
       if ($prev_link){
           echo '<li>'.$prev_link .'</li>';
       }
       if ($next_link){
           echo '<li>'.$next_link .'</li>';
       }
       echo '</ul></nav>';
   }
?>
<!-- html stuff --> 
<?php get_footer(); ?>

There are two blog posts on the development install I am working with. But neither of them display with the navigational links.

Share Improve this question asked Jun 27, 2019 at 1:41 Matthew Brown aka Lord MattMatthew Brown aka Lord Matt 1,0683 gold badges13 silver badges34 bronze badges 2
  • Are they not appearing when you view the individual posts? Or when visiting the blog? – Jacob Peattie Commented Jun 27, 2019 at 1:53
  • Not on individual posts, archives, anything. – Matthew Brown aka Lord Matt Commented Jun 27, 2019 at 2:08
Add a comment  | 

2 Answers 2

Reset to default 1

As suggested by the default link labels, "Older Entries" and "Newer Entries", get_next_posts_link() and get_next_posts_link() are for outputting links between pages of posts. If you only have 2 posts, and your posts per page setting is set to anything higher than 1, then you're not going to have more than one page of any archive or your blog, so the links won't appear.

If you want links between single posts, then the correct functions are get_next_post_link() and get_previous_post_link(). Those functions do need to be in the loop.

While it's technically possible to use index.php for single posts and archives/the blog, you can avoid some confusion by starting with at least index.php for your lists of posts, and singular.php for single posts and pages. Use get_next_posts_link() and get_next_posts_link() for links between pages in index.php, outside the loop, but use get_next_post_link() and get_previous_post_link() for links between single posts in singular.php, inside the loop.

Put the code block with all the prev/next links code inside the loop.

The get_previous_posts_link() function relies on the current post ID, and outside of the loop that post ID is not available.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745366868a4624621.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信