get_adjacent_post_rel_link

Retrieves the adjacent post relational link.

Description

(string|void) get_adjacent_post_rel_link( , (constant) $in_same_term = false, (string) $excluded_terms = '', (constant) $previous = true, (string) $taxonomy = 'category' ); 

Can either be next or previous post relational link.

Returns (string|void)

The adjacent post relational link URL.

Parameters (5)

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

Usage

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

Defined (1)

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

/wp-includes/link-template.php  
  1. function get_adjacent_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) { 
  2. if ( $previous && is_attachment() && $post = get_post() ) 
  3. $post = get_post( $post->post_parent ); 
  4. else 
  5. $post = get_adjacent_post( $in_same_term, $excluded_terms, $previous, $taxonomy ); 
  6.  
  7. if ( empty( $post ) ) 
  8. return; 
  9.  
  10. $post_title = the_title_attribute( array( 'echo' => false, 'post' => $post ) ); 
  11.  
  12. if ( empty( $post_title ) ) 
  13. $post_title = $previous ? __( 'Previous Post' ) : __( 'Next Post' ); 
  14.  
  15. $date = mysql2date( get_option( 'date_format' ), $post->post_date ); 
  16.  
  17. $title = str_replace( '%title', $post_title, $title ); 
  18. $title = str_replace( '%date', $date, $title ); 
  19.  
  20. $link = $previous ? "<link rel='prev' title='" : "<link rel='next' title='"; 
  21. $link .= esc_attr( $title ); 
  22. $link .= "' href='" . get_permalink( $post ) . "' />\n"; 
  23.  
  24. $adjacent = $previous ? 'previous' : 'next'; 
  25.  
  26. /** 
  27. * Filters the adjacent post relational link. 
  28. * The dynamic portion of the hook name, `$adjacent`, refers to the type 
  29. * of adjacency, 'next' or 'previous'. 
  30. * @since 2.8.0 
  31. * @param string $link The relational link. 
  32. */ 
  33. return apply_filters( "{$adjacent}_post_rel_link", $link );