WordPress函数文档get_boundary_post()
获取最新/最旧文章 描述 译文 根据发布日期获取第一篇或最后一篇文章。 原文 Get the first or…
获取最新/最旧文章
描述
译文
根据发布日期获取第一篇或最后一篇文章。
原文
Get the first or last post by publish date
用法
<?php get_boundary_post( $in_same_cat, $excluded_categories, $start ); ?>
参数
$in_same_cat
(boolean) (可选) Whether post should be in same category.
默认值: false
$excluded_categories
(string) (可选) Excluded categories IDs.
默认值: ”
$start
(boolean) (可选) True to begin at the beginning
默认值: true
返回值
- Post object if successful.
- Null if global $post is not set.
- Empty string if no corresponding post exists.
注意
get_boundary_post() will set the post pointer to the first post.
历史
添加于 版本: 2.8.0
源文件
get_boundary_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
|
/* ———————————-
* wordpress函数 kim收集
* ———————————- */
/**
* Retrieve boundary post.
*
* Boundary being either the first or last post by publish date within the constraints specified
* by $in_same_term or $excluded_terms.
*
* @since 2.8.0
*
* @param bool $in_same_term Optional. Whether returned 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 $start Optional. Whether to retrieve first or last post.
* @param string $taxonomy Optional. Taxonomy, if $in_same_term is true. Default ‘category’.
* @return null|array Array containing the boundary post object if successful, null otherwise.
*/
function get_boundary_post( $in_same_term = false, $excluded_terms = ”, $start = true, $taxonomy = ‘category’ ) {
$post = get_post();
if ( ! $post || ! is_single() || is_attachment() || ! taxonomy_exists( $taxonomy ) )
return null;
$query_args = array(
‘posts_per_page’ => 1,
‘order’ => $start ? ‘ASC’ : ‘DESC’,
‘update_post_term_cache’ => false,
‘update_post_meta_cache’ => false
);
$term_array = array();
if ( ! is_array( $excluded_terms ) ) {
if ( ! empty( $excluded_terms ) )
$excluded_terms = explode( ‘,’, $excluded_terms );
else
$excluded_terms = array();
}
if ( $in_same_term || ! empty( $excluded_terms ) ) {
if ( $in_same_term )
$term_array = wp_get_object_terms( $post->ID, $taxonomy, array( ‘fields’ => ‘ids’ ) );
if ( ! empty( $excluded_terms ) ) {
$excluded_terms = array_map( ‘intval’, $excluded_terms );
$excluded_terms = array_diff( $excluded_terms, $term_array );
$inverse_terms = array();
foreach ( $excluded_terms as $excluded_term )
$inverse_terms[] = $excluded_term * –1;
$excluded_terms = $inverse_terms;
}
$query_args[ ‘tax_query’ ] = array( array(
‘taxonomy’ => $taxonomy,
‘terms’ => array_merge( $term_array, $excluded_terms )
) );
}
return get_posts( $query_args );
}
|
- 原文:http://codex.wordpress.org/Function_Reference/get_boundary_post
类别:WordPress函数文档、
本文收集自互联网,转载请注明来源。
如有侵权,请联系 wper_net@163.com 删除。
评论功能已经关闭!