检查当前类别是否有子类别

时间:2015-01-28 作者:Joaquín L. Robles

我在WP Knowledge Base 主题以显示父类别的子类别。问题是,这只适用于层次结构的第一级,所以我想更改if子句,以检查当前类别是否有子级,但我不知道如何做到这一点,知道吗?谢谢

global $term_meta, $cat, $cat_id, $wp_query;

// Check if the current category is not a first level category
// This will happen if the current category does not have any child
// If this is the case, then we simply show all it\'s posts
// Instead of the nice knowledgebase type things
if ( $cat->parent != \'0\' ) {

5 个回复
最合适的回答,由SO网友:Howdy_McGee 整理而成

您可以使用这个简单的函数调用,它返回TRUEFALSE 取决于if$children 是否为空数组。

/**
 * Check if given term has child terms
 *
 * @param Integer $term_id
 * @param String $taxonomy
 *
 * @return Boolean
 */
function category_has_children( $term_id = 0, $taxonomy = \'category\' ) {
    $children = get_categories( array( 
        \'child_of\'      => $term_id,
        \'taxonomy\'      => $taxonomy,
        \'hide_empty\'    => false,
        \'fields\'        => \'ids\',
    ) );
    return ( $children );
}
所以如果你只使用内置的post 您可以这样调用函数的类别:category_has_children( 17 );

如果需要测试自定义分类法,其工作原理几乎相同,只需传入额外的参数“taxonomy slug”:category_has_children( 7, \'my_taxonomy\' );\'

在IF语句中调用它:

if( $cat->parent != 0 && ! category_has_children( $cat->term_id ) )

SO网友:Pieter Goosen

已经有了用于此的内置函数,无需为此创建自定义函数。调用该函数get_term_children() 并将返回

如果给定术语有子术语,则子术语数组

如果未找到子项,则为空数组

  • WP_Error 对象,如果分类不存在

  • 记住这一点,包装get_term_children() 如果只需要布尔值(如条件标记),则根据返回的值返回true或false

    function has_term_have_children( $term_id = \'\', $taxonomy = \'category\' )
    {
        // Check if we have a term value, if not, return false
        if ( !$term_id ) 
            return false;
    
        // Get term children
        $term_children = get_term_children( filter_var( $term_id, FILTER_VALIDATE_INT ), filter_var( $taxonomy, FILTER_SANITIZE_STRING ) );
    
        // Return false if we have an empty array or WP_Error object
        if ( empty( $term_children ) || is_wp_error( $term_children ) )
        return false;
    
        return true;
    }
    
    如果分类法不是category, 您将得到一个boolen值,就像内置条件标记一样,如果术语有子项,则返回true,如果没有子项,则返回false。

    if ( has_term_have_children( 21 ) ) {
        // Do something if term 21 have children
    }
    

    SO网友:Shreyo Gi

    获取子类别的名称。我使用了@Howdy\\u McGee的函数来更快地;迭代过程。

    function category_has_children( $term_id = 0, $post_type = \'post\', $taxonomy = \'category\' ) {
    
        $children = get_categories( array( \'child_of\' => $term_id, \'type\' => $post_type, \'taxonomy\' => $taxonomy, \'order\' => \'ASC\', \'orderby\' => \'name\' ) );
        if($children){
        echo \'<ul>\';
        foreach ($children as $value) {
            echo \'<li>\';
                echo \'<a href="\'.get_bloginfo("siteurl").\'/category/\'.$value->slug.\'" >\'.$value->name.\'</a>\';
            echo \'</li>\';
            }
        echo \'</ul>\';
        }
    }
    
    $cId = get_cat_id(\'cms\'); /* cms is the parent category */
    category_has_children($cId, \'post\', \'category\');
    
    这将列出父类别的所有子类别(子类别)。

    SO网友:Randy Kilwag

    这就是我使用的。变量$cat是您正在检查的类别ID:

    $categories = get_categories($cat);
    
    if (!empty($categories)) {
        // This Category has children
        }
    else {
        // This category has no children
        }
    

    SO网友:Sudip Debnath

    使用此功能cat_has_subcat(\'paste cat id\') 检查类别是否有子类别

    //paste it in your function.php
    
    //start
    
    
    function cat_has_subcat($catId){
        $args = array(
            \'taxonomy\' => \'product_cat\',
            \'orderby\' => \'name\',
            \'parent\' => $catId,
            \'show_count\' => 0,
            \'pad_counts\' => 0,
            \'hierarchical\' => 1,
            \'title_li\' => \'\',
            \'hide_empty\' => false
        );
    
        $cats = get_categories($args);
        if(!empty($cats)){
            return $catId;
        }
    }
    
    //end
    

    结束