get_adjacent_post_link

Retrieves the adjacent post link.

Description

(string) get_adjacent_post_link( (string) $format, (string) $link, (constant) $in_same_term = false, (string) $excluded_terms = '', (constant) $previous = true, (string) $taxonomy = 'category' ); 

Can be either next post link or previous.

Returns (string)

The link URL of the previous or next post in relation to the current post.

Parameters (6)

0. $format (string)
Link anchor format.
1. $link (string)
Link permalink format.
2. $in_same_term — Optional. (constant) => false
Whether link should be in a same taxonomy term. Default false.
3. $excluded_terms — Optional. (string) => ''
Array or comma-separated list of excluded terms IDs. Default empty.
4. $previous — Optional. (constant) => true
Whether to display link to previous or next post. Default true.
5. $taxonomy — Optional. (string) => 'category'
Taxonomy, if $in_same_term is true. Default category..

Usage

  1. if ( !function_exists( 'get_adjacent_post_link' ) ) { 
  2. require_once ABSPATH . WPINC . '/link-template.php'; 
  3.  
  4. // Link anchor format. 
  5. $format = ''; 
  6.  
  7. // Link permalink format. 
  8. $link = ''; 
  9.  
  10. // Optional. Whether link should be in a same taxonomy term. Default false. 
  11. $in_same_term = false; 
  12.  
  13. // Optional. Array or comma-separated list of excluded terms IDs. Default empty. 
  14. $excluded_terms = ''; 
  15.  
  16. // Optional. Whether to display link to previous or next post. Default true. 
  17. $previous = true; 
  18.  
  19. // Optional. Taxonomy, if $in_same_term is true. Default 'category'. 
  20. $taxonomy = 'category'; 
  21.  
  22. // NOTICE! Understand what this does before running. 
  23. $result = get_adjacent_post_link($format, $link, $in_same_term, $excluded_terms, $previous, $taxonomy); 
  24.  

Defined (1)

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

/wp-includes/link-template.php  
  1. function get_adjacent_post_link( $format, $link, $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) { 
  2. if ( $previous && is_attachment() ) 
  3. $post = get_post( get_post()->post_parent ); 
  4. else 
  5. $post = get_adjacent_post( $in_same_term, $excluded_terms, $previous, $taxonomy ); 
  6.  
  7. if ( ! $post ) { 
  8. $output = ''; 
  9. } else { 
  10. $title = $post->post_title; 
  11.  
  12. if ( empty( $post->post_title ) ) 
  13. $title = $previous ? __( 'Previous Post' ) : __( 'Next Post' ); 
  14.  
  15. /** This filter is documented in wp-includes/post-template.php */ 
  16. $title = apply_filters( 'the_title', $title, $post->ID ); 
  17.  
  18. $date = mysql2date( get_option( 'date_format' ), $post->post_date ); 
  19. $rel = $previous ? 'prev' : 'next'; 
  20.  
  21. $string = '<a href="' . get_permalink( $post ) . '" rel="'.$rel.'">'; 
  22. $inlink = str_replace( '%title', $title, $link ); 
  23. $inlink = str_replace( '%date', $date, $inlink ); 
  24. $inlink = $string . $inlink . '</a>'; 
  25.  
  26. $output = str_replace( '%link', $inlink, $format ); 
  27.  
  28. $adjacent = $previous ? 'previous' : 'next'; 
  29.  
  30. /** 
  31. * Filters the adjacent post link. 
  32. * The dynamic portion of the hook name, `$adjacent`, refers to the type 
  33. * of adjacency, 'next' or 'previous'. 
  34. * @since 2.6.0 
  35. * @since 4.2.0 Added the `$adjacent` parameter. 
  36. * @param string $output The adjacent post link. 
  37. * @param string $format Link anchor format. 
  38. * @param string $link Link permalink format. 
  39. * @param WP_Post $post The adjacent post. 
  40. * @param string $adjacent Whether the post is previous or next. 
  41. */ 
  42. return apply_filters( "{$adjacent}_post_link", $output, $format, $link, $post, $adjacent );