get_boundary_post()
get_boundary_post( bool $in_same_term = false, array|st…
get_boundary_post( bool $in_same_term = false, array|string $excluded_terms = ”, bool $start = true, string $taxonomy = ‘category’ )
检索边界柱。
Retrieves the boundary post.
检索边界柱。
Retrieves the boundary post.
目录锚点:#说明#参数#返回#源码
说明(Description)
边界是在$in-same_terms或$excluded_terms指定的约束范围内,按发布日期发布的第一篇或最后一篇文章。
参数(Parameters)
| 参数 | 类型 | 必填 | 说明 |
|---|---|---|---|
| $in_same_term | (bool) | 可选 | 返回的post是否应该使用相同的分类术语。 |
| $excluded_terms | (array | string) | 可选 | 排除的术语ID的数组或逗号分隔列表。 |
| $start | (bool) | 可选 | 是检索第一篇还是最后一篇文章。默认为真 |
| $taxonomy | (string) | 可选 | 分类法,如果$in_相同的术语是真的。 |
返回(Return)
(null|array)如果成功,则包含boundary post对象的数组,否则为null。
源码(Source)
/**
* 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 );
}| 更新版本 | 源码位置 | 使用 | 被使用 |
|---|---|---|---|
| 2.8.0 | wp-includes/link-template.php:2022 | 1 function | 6 |
类别:WordPress 函数手册、
本文收集自互联网,转载请注明来源。
如有侵权,请联系 wper_net@163.com 删除。
还没有任何评论,赶紧来占个楼吧!