2016-05-03 52 views

回答

1

該函數返回的孩子true在上述情況。否則爲false

<?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; 
    } 
} 
相關問題