🤪Kiểm tra bài viết có thuộc chuyên mục, Check post is in the category dakhoathienhoa.com.vn (ok)

https://stackoverflow.com/questions/67111152/check-if-the-current-post-is-within-a-parent-category-or-its-subcategories

Ví dụ 1: Thực hiện trên phongkhamdakhoathienhoa.com.vn (ok)

if ( ! function_exists( 'post_is_in_a_subcategory' ) ) {
function post_is_in_a_subcategory( $categories, $_post = null ) {
    if(has_category( $categories,$_post)) return true;
    return false;
  }
}
<?php  
    if (post_is_in_a_subcategory( array( 46),get_the_ID() )) {
        $includes = [447,444,431];
        echo "aaaaaaaaa1";
    }else {
        $includes = [413,427,434];
        echo "aaaaaaaaa2";
    }
    echo "<pre>";
        var_export($includes);
    echo "</pre>";
?>

Ví dụ 2: Không hiểu sao thực hiện trên phongkhamdakhoathienhoa.com.vn không chính xác

if ( ! function_exists( 'post_is_in_a_subcategory' ) ) {
function post_is_in_a_subcategory( $categories, $_post = null ) {
    foreach ( (array) $categories as $category ) {
        // get_term_children() only accepts integer ID
        $subcats = get_term_children( (int) $category, 'category' );
        if ( $subcats && in_category( $subcats, $_post ) )
            return true;
    }
    return false;
  }
}
<?php  
    if (post_is_in_a_subcategory( array( 3 ) )):
        echo "aaaaaa1";
    else :
        echo "aaaaaa2";
    endif;
?>

Last updated