get_the_posts_navigation

Returns the navigation to next/previous set of posts, when applicable.

Description

(string) get_the_posts_navigation( (array) $args = array() ); 

Returns (string)

Markup for posts links.

Parameters (1)

0. $args — Optional. (array) => array()
Default posts navigation arguments. Default empty array.

Options

  • prev_text (string) => 'Older posts'

    Anchor text to display in the previous posts link.

  • next_text (string) => 'Newer posts'

    Anchor text to display in the next posts link.

array(

    /**
     * Anchor text to display in the previous posts link.
     *
     * @type string
     * @default 'Older posts'
     */
    'prev_text' => 'Older posts',

    /**
     * Anchor text to display in the next posts link.
     *
     * @type string
     * @default 'Newer posts'
     */
    'next_text' => 'Newer posts'
);        


Usage

  1. if ( !function_exists( 'get_the_posts_navigation' ) ) { 
  2. require_once ABSPATH . WPINC . '/link-template.php'; 
  3.  
  4. // Optional. Default posts navigation arguments. Default empty array. 
  5. $args = array( 
  6. 'prev_text' => 'Older posts', 
  7. 'next_text' => 'Newer posts' 
  8. ); 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = get_the_posts_navigation($args); 
  12.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/link-template.php  
  1. function get_the_posts_navigation( $args = array() ) { 
  2. $navigation = ''; 
  3.  
  4. // Don't print empty markup if there's only one page. 
  5. if ( $GLOBALS[wp_query]->max_num_pages > 1 ) { 
  6. $args = wp_parse_args( $args, array( 
  7. 'prev_text' => __( 'Older posts' ),  
  8. 'next_text' => __( 'Newer posts' ),  
  9. 'screen_reader_text' => __( 'Posts navigation' ),  
  10. ) ); 
  11.  
  12. $next_link = get_previous_posts_link( $args['next_text'] ); 
  13. $prev_link = get_next_posts_link( $args['prev_text'] ); 
  14.  
  15. if ( $prev_link ) { 
  16. $navigation .= '<div class="nav-previous">' . $prev_link . '</div>'; 
  17.  
  18. if ( $next_link ) { 
  19. $navigation .= '<div class="nav-next">' . $next_link . '</div>'; 
  20.  
  21. $navigation = _navigation_markup( $navigation, 'posts-navigation', $args['screen_reader_text'] ); 
  22.  
  23. return $navigation;