Make WordPress Core

Opened 6 years ago

Closed 6 years ago

Last modified 6 years ago

#37260 closed feature request (invalid)

Request: Functions to check if there's a 2nd page

Reported by: henry.wright Owned by:
Milestone: Priority: normal
Severity: normal Version: 4.5.3
Component: General Keywords: close
Focuses: template Cc:

Description

Sometimes it's useful to wrap a pagination function in HTML:

<nav><?php some_wp_pag_function(); ?></nav>

But if there's no 2nd page, we're left with markup such as:

<nav> </nav>

It would be useful to have a set of functions available which check if there's a 2nd page so we could do stuff like this:

if ( is_there_a_second_page() ) { ?>
    <nav><?php some_wp_pag_function(); ?></nav>
<?php }

Things that can be paginated which spring to mind are:

  • Posts
  • Comments
  • A single post via <!-- nextpage -->

Change History (6)

#1 @SergeyBiryukov
6 years ago

  • Focuses template added

#2 @Jonnyauk
6 years ago

I think you should probably check out the functions get_previous_post_link() and get_next_post_link() - that's what I've used to avoid the empty markup as you outline in your example. Visit the links below for full documentation.

https://developer.wordpress.org/reference/functions/get_next_post_link/
https://developer.wordpress.org/reference/functions/get_previous_post_link/

This would allow you to do something like this:

<?php
$prev_link = get_previous_post_link('&laquo; %link');
$next_link = get_next_post_link('&laquo; %link');
?>

Once you have those set as variables, you can then conditionally test against, and display those variables, so a new function as you suggest is not really required ;) Also worth noting is that the first parameter is the 'format' of the output link, so you can do things like below - and it will only output if a link exists:

<?php
echo get_previous_post_link('<p class="link-previous-post">&laquo; %link</p>');
echo get_next_post_link('<p class="link-next-post">&laquo; %link</p>');
?>

#3 @henry.wright
6 years ago

Hi Jonny,

That's a pretty neat work around.

#4 @Jonnyauk
6 years ago

  • Resolution set to invalid
  • Status changed from new to closed

You were right in trying to avoid empty markup - no-one wants that sir ;)

I hope this works out for you - it would certainly negate your request for a new function. There is so many cool, handy functions like this hiding away in WordPress ready to use ;)

#5 @ocean90
6 years ago

  • Milestone Awaiting Review deleted

#6 @Jonnyauk
6 years ago

  • Keywords close added
  • Type changed from defect (bug) to feature request
Note: See TracTickets for help on using tickets.