Languages:
中文(简体) •
English •
Italiano •
한국어 •
日本語
Português do Brasil •
(Add your language)
Description
Retrieve adjacent post. Can either be next or previous post.
Usage
<?php get_adjacent_post( $in_same_term, $excluded_terms, $previous, $taxonomy ) ?>
Parameters
- $in_same_term
- (boolean) (optiona) Whether post should be in a same taxonomy term.
- Default: false
- $excluded_terms
- (array or string) (optional) Array or comma-separated list of excluded term IDs.
- Default: ''
- $previous
- (boolean) (optional) Whether to retrieve previous post.
- Default: true
- $taxonomy
- (string) (optional) Taxonomy name, if $in_same_term is true.
- Default: 'category'
Return Values
- Post object if successful.
- Null if global $post is not set.
- Empty string if no corresponding post exists.
Examples
Link to previous post in the same taxonomy with Post Title for text.
<?php $prev_post = get_adjacent_post( true, '', true, 'taxonomy_slug' ); ?>
<?php if ( is_a( $prev_post, 'WP_Post' ) ) { ?>
<a href="<?php echo get_permalink( $prev_post->ID ); ?>"><?php echo get_the_title( $prev_post->ID ); ?></a>
<?php } ?>
Link to next post in the same taxonomy with Post Title for text.
<?php $next_post = get_adjacent_post( true, '', false, 'taxonomy_slug' ); ?>
<?php if ( is_a( $next_post, 'WP_Post' ) ) { ?>
<a href="<?php echo get_permalink( $next_post->ID ); ?>"><?php echo get_the_title( $next_post->ID ); ?></a>
<?php } ?>
Notes
- Uses global: (object) $post
- Uses global: (object) $wpdb
Filters
$adjacent is either 'previous' or 'next'.
- "get_{$adjacent}_post_join":
$join, $in_same_cat, $excluded_categories
- "get_{$adjacent}_post_where":
$wpdb->prepare("WHERE p.post_date $op %s AND p.post_type = %s AND p.post_status = 'publish' $posts_in_ex_cats_sql", $current_post_date, $post->post_type), $in_same_cat, $excluded_categories
- "get_{$adjacent}_post_sort":
"ORDER BY p.post_date $order LIMIT 1"
Change Log
Since: 2.5.0
Source File
get_adjacent_post() is located in wp-includes/link-template.php
.
Related
get_next_post(), get_previous_post()