On this page
get_adjacent_post_rel_link( string $title = '%title', bool $in_same_term = false, int[]|string $excluded_terms = '', bool $previous = true, string $taxonomy = 'category' ): string|void
Retrieves the adjacent post relational link.
Description
Can either be next or previous post relational link.
Parameters
$titlestring Optional-
Link title format. Default
'%title'.Default:
'%title' $in_same_termbool Optional-
Whether link should be in a same taxonomy term.
Default:
false $excluded_termsint[]|string Optional-
Array or comma-separated list of excluded term IDs.
Default:
'' $previousbool Optional-
Whether to display link to previous or next post.
Default:
true $taxonomystring Optional-
Taxonomy, if $in_same_term is true. Default
'category'.Default:
'category'
Return
string|void The adjacent post relational link URL.
Source
File: wp-includes/link-template.php. View all references
function get_adjacent_post_rel_link( $title = '%title', $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
$post = get_post();
if ( $previous && is_attachment() && $post ) {
$post = get_post( $post->post_parent );
} else {
$post = get_adjacent_post( $in_same_term, $excluded_terms, $previous, $taxonomy );
}
if ( empty( $post ) ) {
return;
}
$post_title = the_title_attribute(
array(
'echo' => false,
'post' => $post,
)
);
if ( empty( $post_title ) ) {
$post_title = $previous ? __( 'Previous Post' ) : __( 'Next Post' );
}
$date = mysql2date( get_option( 'date_format' ), $post->post_date );
$title = str_replace( '%title', $post_title, $title );
$title = str_replace( '%date', $date, $title );
$link = $previous ? "<link rel='prev' title='" : "<link rel='next' title='";
$link .= esc_attr( $title );
$link .= "' href='" . get_permalink( $post ) . "' />\n";
$adjacent = $previous ? 'previous' : 'next';
/**
* Filters the adjacent post relational link.
*
* The dynamic portion of the hook name, `$adjacent`, refers to the type
* of adjacency, 'next' or 'previous'.
*
* Possible hook names include:
*
* - `next_post_rel_link`
* - `previous_post_rel_link`
*
* @since 2.8.0
*
* @param string $link The relational link.
*/
return apply_filters( "{$adjacent}_post_rel_link", $link );
}
Hooks
- apply_filters( "{$adjacent}_post_rel_link",
string $link ) -
Filters the adjacent post relational link.
Related
Uses
| Uses | Description |
|---|---|
| is_attachment() wp-includes/query.php | Determines whether the query is for an existing attachment page. |
| mysql2date() wp-includes/functions.php | Converts given MySQL date string into a different format. |
| get_adjacent_post() wp-includes/link-template.php | Retrieves the adjacent post. |
| the_title_attribute() wp-includes/post-template.php | Sanitizes the current title when retrieving or displaying. |
| __() wp-includes/l10n.php | Retrieves the translation of $text. |
| esc_attr() wp-includes/formatting.php | Escaping for HTML attributes. |
| get_permalink() wp-includes/link-template.php | Retrieves the full permalink for the current post or post ID. |
| apply_filters() wp-includes/plugin.php | Calls the callback functions that have been added to a filter hook. |
| get_option() wp-includes/option.php | Retrieves an option value based on an option name. |
| get_post() wp-includes/post.php | Retrieves post data given a post ID or post object. |
Used By
| Used By | Description |
|---|---|
| adjacent_posts_rel_link() wp-includes/link-template.php | Displays the relational links for the posts adjacent to the current post. |
| next_post_rel_link() wp-includes/link-template.php | Displays the relational link for the next post adjacent to the current post. |
| prev_post_rel_link() wp-includes/link-template.php | Displays the relational link for the previous post adjacent to the current post. |
Changelog
| Version | Description |
|---|---|
| 2.8.0 | Introduced. |
© 2003–2022 WordPress Foundation
Licensed under the GNU GPLv2+ License.
https://developer.wordpress.org/reference/functions/get_adjacent_post_rel_link