0

I'm working on implementing a custom pagination for a website. Unlike default Wordpress pagination with "Older" and "Newer" links, this one requires to show the title, the date and the teaser of the next or previous post. Please check attached image:

enter image description here

When it comes to this part, I always do something like this:

<?php if ($the_query->max_num_pages > 1) { // check if the max number     of pages is greater than 1  ?>
  <nav class="prev-next-posts">
    <div class="prev-posts-link">
      <?php echo get_next_posts_link( 'Older Entries', $the_query->max_num_pages ); // display older posts link ?>
    </div>
    <div class="next-posts-link">
      <?php echo get_previous_posts_link( 'Newer Entries' ); // display newer posts link ?>
    </div>
  </nav>
<?php } ?>

But as you might guess, this only shows a link to the "Older" or "Newer" entries. How could I retrieve the title, date and teaser of these associated posts?

Thanks a lot for your help.

  • Is this on a normal archive page, like the homepage, or on a single post page? – Pieter Goosen Nov 10 '15 at 5:17
0

I assume you're talking about single posts, not archives - get_next_posts_link gets the next archive page, get_next_post_link gets the post next to the current one (note the plural difference).

If so, the next/previous post link functions are just wrappers for get_adjacent_post():

<?php if ( $the_query->max_num_pages > 1 ) : ?>

    <nav class="prev-next-posts">
        <?php if ( $post = get_adjacent_post( false /* In same term? */, '' /* Excluded terms */, true /* Previous */ ) ) : setup_postdata( $post ) ?>

            <div class="prev-posts-link">
                <a href="<?php the_permalink() ?>"><?php the_title() ?></a>

                <?php the_excerpt() ?>
            </div>

        <?php endif ?>

        <?php if ( $post = get_adjacent_post( false, '', false ) ) : setup_postdata( $post ) ?>

            <div class="next-posts-link">
                <a href="<?php the_permalink() ?>"><?php the_title() ?></a>

                <?php the_excerpt() ?>
            </div>

        <?php endif ?>

        <?php wp_reset_postdata() ?>
    </nav>

<?php endif ?>
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.