WordPress函数文档get_adjacent_post()
获取相邻文章 描述 译文 检索相邻的文章,可以是上一篇或下一篇。 1 2 3 4 5 6 7 8 9 /* &…
获取相邻文章
描述
译文
检索相邻的文章,可以是上一篇或下一篇。
1
2
3
4
5
6
7
8
9
|
/* ———————————-
* wordpress函数 kim收集
* ———————————- */
function wphun_insert_query_vars( $vars )
{
//新增参数
array_push($vars, ‘function_name’);
return $vars;
}
|
原文
Retrieve adjacent post. Can either be next or previous post.
用法
1
2
3
4
|
/* ———————————-
* wordpress函数 kim收集
* ———————————- */
<?php get_adjacent_post( $in_same_term, $excluded_terms, $previous, $taxonomy ) ?>
|
参数
$in_same_term
(boolean) (可选)文章是否在同一分类。
默认值: false
$excluded_terms
(array or string) (可选) 排除分类的ID。
默认值: ”
$previous
(boolean) (可选)是否检索之前的文章。
默认值: true
$taxonomy
(string) (可选) 限定的分类名称,如果 $in_same_term 设置为 true。
默认值: ‘category’
返回值
示例
获取同分类下的上一篇文章。
1
2
3
4
5
6
7
|
/* ———————————-
* wordpress函数 kim收集
* ———————————- */
<?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 } ?>
|
获取同分类下的下一篇文章。
1
2
3
4
5
6
7
|
/* ———————————-
* wordpress函数 kim收集
* ———————————- */
<?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 } ?>
|
注意
- 使用 global: (object) $post
- 使用 global: (object) $wpdb
Filters过滤器
$adjacent 是 ‘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”
历史
添加于 版本: 2.5.0
源文件
get_adjacent_post() 函数的代码位于 wp-includes/link-template.php
.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
|
/* ———————————-
* wordpress函数 kim收集
* ———————————- */
/**
* Retrieve adjacent post.
*
* Can either be next or previous post.
*
* @since 2.5.0
*
* @global wpdb $wpdb
*
* @param bool $in_same_term Optional. Whether post should be in a same taxonomy term.
* @param array|string $excluded_terms Optional. Array or comma-separated list of excluded term IDs.
* @param bool $previous Optional. Whether to retrieve previous post.
* @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default ‘category’.
* @return null|string|WP_Post Post object if successful. Null if global $post is not set. Empty string if no corresponding post exists.
*/
function get_adjacent_post( $in_same_term = false, $excluded_terms = ”, $previous = true, $taxonomy = ‘category’ ) {
global $wpdb;
if ( ( ! $post = get_post() ) || ! taxonomy_exists( $taxonomy ) )
return null;
$current_post_date = $post->post_date;
$join = ”;
$where = ”;
if ( $in_same_term || ! empty( $excluded_terms ) ) {
$join = ” INNER JOIN $wpdb->term_relationships AS tr ON p.ID = tr.object_id INNER JOIN $wpdb->term_taxonomy tt ON tr.term_taxonomy_id = tt.term_taxonomy_id”;
$where = $wpdb->prepare( “AND tt.taxonomy = %s”, $taxonomy );
if ( ! empty( $excluded_terms ) && ! is_array( $excluded_terms ) ) {
// back-compat, $excluded_terms used to be $excluded_terms with IDs separated by ” and “
if ( false !== strpos( $excluded_terms, ‘ and ‘ ) ) {
_deprecated_argument( __FUNCTION__, ‘3.3’, sprintf( __( ‘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 );
}
if ( $in_same_term ) {
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( $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;
}
/*
* Results should include private posts belonging to the current user, or private posts where the
* current user has the ‘read_private_posts’ cap.
*/
$private_states = get_post_stati( array( ‘private’ => true ) );
$where .= ” AND ( p.post_status = ‘publish’”;
foreach ( (array) $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’”;
}
$adjacent = $previous ? ‘previous’ : ‘next’;
$op = $previous ? ‘<‘ :=“” ‘=””>’;
$order = $previous ? ‘DESC’ : ‘ASC’;
/**
* Filter the JOIN clause in the SQL for an adjacent post query.
*
* The dynamic portion of the hook name, `$adjacent`, refers to the type
* of adjacency, ‘next’ or ‘previous’.
*
* @since 2.5.0
*
* @param string $join The JOIN clause in the SQL.
* @param bool $in_same_term Whether post should be in a same taxonomy term.
* @param array $excluded_terms Array of excluded term IDs.
*/
$join = apply_filters( “get_{$adjacent}_post_join”, $join, $in_same_term, $excluded_terms );
/**
* Filter the WHERE clause in the SQL for an adjacent post query.
*
* The dynamic portion of the hook name, `$adjacent`, refers to the type
* of adjacency, ‘next’ or ‘previous’.
*
* @since 2.5.0
*
* @param string $where The `WHERE` clause in the SQL.
* @param bool $in_same_term Whether post should be in a same taxonomy term.
* @param array $excluded_terms Array of excluded term IDs.
*/
$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 );
/**
* Filter the ORDER BY clause in the SQL for an adjacent post query.
*
* The dynamic portion of the hook name, `$adjacent`, refers to the type
* of adjacency, ‘next’ or ‘previous’.
*
* @since 2.5.0
*
* @param string $order_by The `ORDER BY` clause in the SQL.
*/
$sort = apply_filters( “get_{$adjacent}_post_sort”, “ORDER BY p.post_date $order LIMIT 1” );
$query = “SELECT p.ID FROM $wpdb->posts AS p $join $where $sort”;
$query_key = ‘adjacent_post_’ . md5( $query );
$result = wp_cache_get( $query_key, ‘counts’ );
if ( false !== $result ) {
if ( $result )
$result = get_post( $result );
return $result;
}
$result = $wpdb->get_var( $query );
if ( null === $result )
$result = ”;
wp_cache_set( $query_key, $result, ‘counts’ );
if ( $result )
$result = get_post( $result );
return $result;
}
</‘>
|
相关
get_next_post(), get_previous_post()
- 原文:http://codex.wordpress.org/Function_Reference/get_adjacent_post
类别:WordPress函数文档、
本文收集自互联网,转载请注明来源。
如有侵权,请联系 wper_net@163.com 删除。
评论功能已经关闭!