get_posts_nav_link

Retrieves the post pages link navigation for previous and next pages.

Description

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

Returns (string)

The posts link navigation.

Parameters (1)

0. $args — Optional. (array) => array()
Arguments to build the post pages link navigation.

Options

  • sep (string) => '—'

    Separator character.

  • prelabel (string) => '« Previous Page'

    Link text to display for the previous page link.

array(

    /**
     * Separator character.
     *
     * @type string
     * @default '—'
     */
    'sep' => '—',

    /**
     * Link text to display for the previous page link.
     *
     * @type string
     * @default '« Previous Page'
     */
    'prelabel' => '« Previous Page'
);        


Usage

  1. if ( !function_exists( 'get_posts_nav_link' ) ) { 
  2. require_once ABSPATH . WPINC . '/link-template.php'; 
  3.  
  4. // Optional. Arguments to build the post pages link navigation. 
  5. $args = array( 
  6. 'sep' => '—', 
  7. 'prelabel' => '« Previous Page' 
  8. ); 
  9.  
  10. // NOTICE! Understand what this does before running. 
  11. $result = get_posts_nav_link($args); 
  12.  

Defined (1)

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

/wp-includes/link-template.php  
  1. function get_posts_nav_link( $args = array() ) { 
  2. global $wp_query; 
  3.  
  4. $return = ''; 
  5.  
  6. if ( !is_singular() ) { 
  7. $defaults = array( 
  8. 'sep' => ' — ',  
  9. 'prelabel' => __('« Previous Page'),  
  10. 'nxtlabel' => __('Next Page »'),  
  11. ); 
  12. $args = wp_parse_args( $args, $defaults ); 
  13.  
  14. $max_num_pages = $wp_query->max_num_pages; 
  15. $paged = get_query_var('paged'); 
  16.  
  17. //only have sep if there's both prev and next results 
  18. if ($paged < 2 || $paged >= $max_num_pages) { 
  19. $args['sep'] = ''; 
  20.  
  21. if ( $max_num_pages > 1 ) { 
  22. $return = get_previous_posts_link($args['prelabel']); 
  23. $return .= preg_replace('/&([^#])(?![a-z]{1, 8};)/i', '&$1', $args['sep']); 
  24. $return .= get_next_posts_link($args['nxtlabel']); 
  25. return $return; 
  26.