函数原型:
get_adjacent_post( bool $in_same_term = false, int[]|string $excluded_terms = '', bool $previous = true, string $taxonomy = 'category' ): WP_Post|null|string
获取当前文章的相邻文章,上一篇或下一篇。
参数说明:
$in_same_term,是否要求同一分类。
$exclude_terms,需要排除的分类。
$previous,true 前一篇文章,false 后一篇文章。
$taxonomy,分类,默认:'category',与 $in_same_term 配合使用。
函数源码:
function get_adjacent_post( $in_same_term = false, $excluded_terms = '', $previous = true, $taxonomy = 'category' ) {
global $wpdb;
$post = get_post();
if ( ! $post || ! taxonomy_exists( $taxonomy ) ) {
return null;
}
$current_post_date = $post->post_date;
$join = '';
$where = '';
$adjacent = $previous ? 'previous' : 'next';
if ( ! empty( $excluded_terms ) && ! is_array( $excluded_terms ) ) {
// Back-compat, $excluded_terms used to be $excluded_categories with IDs separated by " and ".
if ( false !== strpos( $excluded_terms, ' and ' ) ) {
_deprecated_argument(
__FUNCTION__,
'3.3.0',
sprintf(
/* translators: %s: The word 'and'. */
__( 'Use commas instead of %s to separate excluded terms.' ),
"'and'"
)
);
$excluded_terms = explode( ' and ', $excluded_terms );
} else {
$excluded_terms = explode( ',', $excluded_terms );
}
$excluded_terms = array_map( 'intval', $excluded_terms );
}
$excluded_terms = apply_filters( "get_{$adjacent}_post_excluded_terms", $excluded_terms );
if ( $in_same_term || ! empty( $excluded_terms ) ) {
if ( $in_same_term ) {
$join .= " INNER JOIN $wpdb->term_relationships AS tr ON p.ID = tr.object_id INNER JOIN $wpdb->term_taxonomy AS tt ON tr.term_taxonomy_id = tt.term_taxonomy_id";
$where .= $wpdb->prepare( 'AND tt.taxonomy = %s', $taxonomy );
if ( ! is_object_in_taxonomy( $post->post_type, $taxonomy ) ) {
return '';
}
$term_array = wp_get_object_terms( $post->ID, $taxonomy, array( 'fields' => 'ids' ) );
// Remove any exclusions from the term array to include.
$term_array = array_diff( $term_array, (array) $excluded_terms );
$term_array = array_map( 'intval', $term_array );
if ( ! $term_array || is_wp_error( $term_array ) ) {
return '';
}
$where .= ' AND tt.term_id IN (' . implode( ',', $term_array ) . ')';
}
if ( ! empty( $excluded_terms ) ) {
$where .= " AND p.ID NOT IN ( SELECT tr.object_id FROM $wpdb->term_relationships tr LEFT JOIN $wpdb->term_taxonomy tt ON (tr.term_taxonomy_id = tt.term_taxonomy_id) WHERE tt.term_id IN (" . implode( ',', array_map( 'intval', $excluded_terms ) ) . ') )';
}
}
// 'post_status' clause depends on the current user.
if ( is_user_logged_in() ) {
$user_id = get_current_user_id();
$post_type_object = get_post_type_object( $post->post_type );
if ( empty( $post_type_object ) ) {
$post_type_cap = $post->post_type;
$read_private_cap = 'read_private_' . $post_type_cap . 's';
} else {
$read_private_cap = $post_type_object->cap->read_private_posts;
}
$private_states = get_post_stati( array( 'private' => true ) );
$where .= " AND ( p.post_status = 'publish'";
foreach ( $private_states as $state ) {
if ( current_user_can( $read_private_cap ) ) {
$where .= $wpdb->prepare( ' OR p.post_status = %s', $state );
} else {
$where .= $wpdb->prepare( ' OR (p.post_author = %d AND p.post_status = %s)', $user_id, $state );
}
}
$where .= ' )';
} else {
$where .= " AND p.post_status = 'publish'";
}
$op = $previous ? '<' : '>';
$order = $previous ? 'DESC' : 'ASC';
$join = apply_filters( "get_{$adjacent}_post_join", $join, $in_same_term, $excluded_terms, $taxonomy, $post );
$where = apply_filters( "get_{$adjacent}_post_where", $wpdb->prepare( "WHERE p.post_date $op %s AND p.post_type = %s $where", $current_post_date, $post->post_type ), $in_same_term, $excluded_terms, $taxonomy, $post );
$sort = apply_filters( "get_{$adjacent}_post_sort", "ORDER BY p.post_date $order LIMIT 1", $post, $order );
$query = "SELECT p.ID FROM $wpdb->posts AS p $join $where $sort";
$key = md5( $query );
$last_changed = wp_cache_get_last_changed( 'posts' );
if ( $in_same_term || ! empty( $excluded_terms ) ) {
$last_changed .= wp_cache_get_last_changed( 'terms' );
}
$cache_key = "adjacent_post:$key:$last_changed";
$result = wp_cache_get( $cache_key, 'posts' );
if ( false !== $result ) {
if ( $result ) {
$result = get_post( $result );
}
return $result;
}
$result = $wpdb->get_var( $query );
if ( null === $result ) {
$result = '';
}
wp_cache_set( $cache_key, $result, 'posts' );
if ( $result ) {
$result = get_post( $result );
}
return $result;
}
包含钩子:
apply_filters( "get_{$adjacent}_post_excluded_terms", int[]|string $excluded_terms )
apply_filters( "get_{$adjacent}_post_join", string $join, bool $in_same_term, int[]|string $excluded_terms, string $taxonomy, WP_Post $post )
apply_filters( "get_{$adjacent}_post_sort", string $order_by, WP_Post $post, string $order )
apply_filters( "get_{$adjacent}_post_where", string $where, bool $in_same_term, int[]|string $excluded_terms, string $taxonomy, WP_Post $post )
使用举例:
<?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 } ?>
获取相同分类下的前一篇文章。
-
WordPress函数:esc_attr__ 转义属性及翻译WordPress函数:esc_attr__ 转义属性及翻译
-
WordPress函数:esc_attr_e 属性转义、翻译、显示WordPress函数:esc_attr_e 属性转义、翻译、显示
-
WordPress函数:esc_attr_x 带上下文的转义属性,翻译显示WordPress函数:esc_attr_x 带上下文的转义属性,翻译显示
-
WP Multilang WordPress翻译插件WP Multilang插件的主要功能是提供多语言支持,使用户能够创建多语言版本的网站,满足不同语言用户的需求。
-
WordPress公司官网主题在众多的WordPress企业官网主题中,追格公司推出了多款关于WordPress企业官网主题作品。我们之前已经分享过一些关于追格的企业官网主题,包括收费和免费版本,这些主题都因其独特的设计和出色的功能而深受用户喜爱。
-
WordPress必备:使用wp_get_theme()函数获取当前主题详情在WordPress中,wp_get_theme() 函数用于获取当前启用的主题或指定主题的信息。这个函数返回一个 WP_Theme 对象,该对象包含了主题的详细信息,如主题名称、版本、模板目录、样式表目录等。
暂无评论,抢个沙发...