如何检查当前帖子或类别是否是某个类别的后代?

我想知道如何检查当前的单个帖子或当前类别是否是某个类别的子级, 具有任何深度。

例如:

Cat1> Cat2> Cat3> Cat4> Post1

检查Post1是否为Cat1的子代

并检查Cat4是否为Cat1的子代


#1


在上述情况下, 此函数返回true。否则为假。

<?php
function post_or_category_descendant_of( $category_id ) {
    if( is_category() && ( is_category( $category_id ) || cat_is_ancestor_of( $category_id, get_queried_object() ) ) ) {
        return true;

    } else if( is_single() ){

        $post_id = get_queried_object()->id;
        $post_categories = get_the_category( $post_id );

        $post_cat_ids = array();

        foreach( $post_categories as $pc ) {
            $post_cat_ids[] = $pc->cat_ID;
        }

        $args = array( 'child_of' => $category_id, 'fields' => 'ids' );
        $target_cat_ids = get_categories( $args );
        $target_cat_ids[] = $category_id;

        if( array_intersect( $post_cat_ids, $target_cat_ids ) ) {
            return true;
        } else {
            return false;
        }

    } else {
        return false;
    }
}

来源:

https://www.srcmini02.com/63183.html

微信公众号
手机浏览(小程序)

Warning: get_headers(): SSL operation failed with code 1. OpenSSL Error messages: error:14090086:SSL routines:ssl3_get_server_certificate:certificate verify failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(): Failed to enable crypto in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57

Warning: get_headers(https://static.shanhubei.com/qrcode/qrcode_viewid_37675.jpg): failed to open stream: operation failed in /mydata/web/wwwshanhubei/web/wp-content/themes/shanhuke/single.php on line 57
0
分享到:
没有账号? 忘记密码?