get_boundary_post
函数
get_boundary_post ( $in_same_term = false, $excluded_terms = '', $start = true, $taxonomy = 'category' )
- 参数
-
-
(bool)
$in_same_term
Optional. Whether returned post should be in a same taxonomy term. Default false.- Required: 否
- Default: false
-
(int[]|string)
$excluded_terms
Optional. Array or comma-separated list of excluded term IDs. Default empty.- Required: 否
- Default: (empty)
-
(bool)
$start
Optional. Whether to retrieve first or last post. Default true- Required: 否
- Default: true
-
(string)
$taxonomy
Optional. Taxonomy, if $in_same_term is true. Default ‘category’.- Required: 否
- Default: ‘category’
-
(bool)
- 返回值
-
- (null|array) Array containing the boundary post object if successful, null otherwise.
- 定义位置
-
-
wp-includes/link-template.php
, line 2143
-
wp-includes/link-template.php
- 引入
- 2.8.0
- 弃用
- –
检索边界文章。
边界是在$in_same_term或$excluded_terms指定的约束条件下,按发布日期计算的第一个或最后一个文章。
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 ); }
声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。