如何检查父类别具有术语元的类别

时间:2018-08-21 22:27:16

标签: wordpress multiple-inheritance categories taxonomy

我正在尝试在Wordpress中实现以下目标:

我的类别中有一个额外的所见即所得字段,设置为术语元。我将其显示在我的category.php模板文件中。 但是,如果该字段为空,则该类别应检查其直接父级,如果发现那里的字段不为空,则应继承该字段。如果直接父母将其清空,请转到更高级别,检查并显示是否找到。我需要深入5个级别,而不是更多。如果整个继承树中的所有字段均为空,则当然不显示任何内容。

到目前为止,这是我的代码,所以您有个好主意。

$category = get_the_category();
        $ancestors = get_ancestors( $category[0]->term_id, 'category' );
        $direct_parent_id = $ancestors[0];
        $topmost_parent_id = $ancestors[1];
        $level = asb_get_cat_level( $cat );

    if(function_exists('get_tax_meta')) { 
        /* First get Section partner fields */
        $cat_part = get_term_meta(get_queried_object_id(),'cat_part_wysiwyg', true);
        $cat_part_parent = get_term_meta($direct_parent_id,'cat_part_wysiwyg', true);
        $cat_part_grandparent = get_term_meta($topmost_parent_id,'cat_part_wysiwyg', true);

            /* Construct output for Section partners */
            $cat_partner = '<div class="category-partners">' . $cat_part . '</div>';
            $cat_partner_parent = '<div class="category-partners"> ' . $cat_part_parent . '</div>';
            $cat_partner_grandparent = '<div class="category-partners">' . $cat_part_grandparent . '</div>';

            /* Start showing Section partners */
                if( !empty($cat_part_grandparent) ) {
                    print_r($cat_partner_grandparent);
                } elseif( !empty($cat_part_parent) ) {
                    print_r($cat_partner_parent); 
                } elseif ( !empty($cat_part) ) {
                    print_r($cat_partner); 
                } else {
                    /* Die */
                }
        } 

加上我的functions.php具有以下代码:

function asb_get_cat_level($id, $type = 'category') {
    return count( get_ancestors($id, $type) );
}

(我在($ level == 1)等条件条件内使用此函数)

到目前为止,只有最高级的类别显示为“部门”伙伴,但子级没有继承它们。

1 个答案:

答案 0 :(得分:0)

我最终使用了这段代码

    // Section Partners 
    function section_partners() {   
        $cat_id = get_query_var('cat');
        $ancestors = get_ancestors( $cat_id, 'category' );

            $parent_one = $ancestors[0];
            $parent_null = $ancestors[1];
            $parent_two = $ancestors[2];                
        // $level = asb_get_cat_level( $cat );

    if(function_exists('get_tax_meta')) { 
        /* First get Section partner fields */
        $cat_part = get_term_meta(get_queried_object_id(),'cat_part_wysiwyg', true);
        $cat_part_parent = get_term_meta($parent_one, 'cat_part_wysiwyg', true);
        $cat_part_grandparent = get_term_meta($parent_null,'cat_part_wysiwyg', true);
        $cat_part_grandgrandparent = get_term_meta($parent_two,'cat_part_wysiwyg', true);


            /* Construct output for Section partners */
            $cat_partner = '<div class="category-partners"><div>' . $cat_part . '</div></div>';
            $cat_partner_parent = '<div class="category-partners"><div>' . $cat_part_parent . '</div></div>';
            $cat_partner_grandparent = '<div class="category-partners"><div>' . $cat_part_grandparent . '</div></div>';
            $cat_partner_grandgrandparent = '<div class="category-partners"><div>' . $cat_part_grandgrandparent . '</div></div>';


            if ( !empty($cat_part) ) { /* if top category has partners */
                    print_r($cat_partner);
            } elseif ( !empty($cat_part_parent) ) { /* if 1 level category has partners */
                    print_r($cat_partner_parent); 
            } elseif ( !empty($cat_part_grandparent) ) { /* if 2 level category has partners */
                    print_r($cat_partner_grandparent); 
            } elseif ( !empty($cat_part_grandgrandparent) ) { /* if 3 level category has partners */
                    print_r($cat_partner_grandgrandparent); 
            } elseif ( empty($cat_part_parent) && !empty($cat_part) ) { /* inherit from top level */
                    print_r($cat_partner); 
            } elseif ( empty($cat_part_parent) && empty($cat_part_grandparent) && !empty($cat_part) ) { /* inherit from 1 level */
                    print_r($cat_partner); 
            } elseif ( empty($cat_part_grandparent) && empty($cat_part_grandgrandparent) && !empty($cat_part_parent) ) { /* inherit from 2 level */
                    print_r($cat_partner_parent); 
            } elseif ( empty($cat_part_parent) && empty($cat_part_grandparent) && empty($cat_part_grandgrandparent) && !empty($cat_part) ) { /* inherit from 2 level */
                    print_r($cat_partner); 
            } else {
                /* Die */
            }   
        } 

    }

但是仍然需要修复PHP错误$ parent_one,$ parent_null和$ parent_two返回空。