如何显示所选分类的分类法父级

时间:2016-07-21 18:19:26

标签: php wordpress taxonomy custom-taxonomy

我想在网站上显示所选分类的父级。为此,我有上面的代码,但问题是它在现在结构的列表中显示所有选定的分类。

global $post;
$features = get_the_terms( $post->ID, 'property-feature' );

if ( !empty( $features ) && is_array( $features ) && !is_wp_error( $features ) ) {
?>
<div class="property-features">
    <?php
    global $inspiry_options;
    $property_features_title = $inspiry_options[ 'inspiry_property_features_title' ];
    if( !empty( $property_features_title ) ) {
        ?><h4 class="fancy-title"><?php echo esc_html( $property_features_title ); ?></h4><?php
    }
    ?>
    <ul class="property-features-list clearfix">
        <?php

        foreach( $parent_term as $single_feature ) {
            echo '<li><a href="' . get_term_link( $single_feature->slug, 'property-feature' ) .'">'. $single_feature->name . '</a></li>';
        }
        ?>
    </ul>
</div>
<?php

}

示例:

父级:选项1,选项2

父母2:选项3,选项4

如果我选择选项2和选项3,我想看

家长:选项2

父母2:选项3

Updare 2

  global $post;
$features = get_the_terms( $post->ID, 'property-feature' );
$featuresss = get_the_terms( $post->ID, 'property-feature' );

// determine the topmost parent of a term
function get_term_top_most_parent($term_id, $taxonomy){
    // start from the current term
    $parent  = get_term_by( 'id', $term_id, $taxonomy);
    // climb up the hierarchy until we reach a term with parent = '0'
    while ($parent->parent != '0'){
        $term_id = $parent->parent;

        $parent  = get_term_by( 'id', $term_id, $taxonomy);
    }
    return $parent;
}

// so once you have this function you can just loop over the results returned by wp_get_object_terms

function hey_top_parents($taxonomy, $results = 1) {
    // get terms for current post
    $terms = wp_get_object_terms( get_the_ID(), $taxonomy );
    $y = get_the_terms($terms->term_id, $taxonomy);
    // set vars
    $top_parent_terms = array();
    foreach ( $terms as $term ) {
        //get top level parent
        $top_parent = get_term_top_most_parent( $term->term_id, $taxonomy );
        //check if you have it in your array to only add it once
        if ( !in_array( $top_parent, $top_parent_terms ) ) {
            $top_parent_terms[] = $top_parent;
        }
    }
    // build output (the HTML is up to you)
        foreach( $top_parent_terms as $single_feature ) {
                echo '<li><a href="' . get_term_link( $single_feature->slug, 'property-feature' ) .'">'. $single_feature->name . '</a></li>';
                foreach( $terms as $single ) {
                    echo '<ul><li><a href="' . get_term_link( $single->slug, 'property-feature' ) .'">'. $single->name . '</a></li></ul>';
                }
            }

    //return $top_parent_terms;

}

我设法显示所选分类法的顶级父级,但现在问题是我现在需要在顶级父级中显示来自该父级的所选分类法

1 个答案:

答案 0 :(得分:3)

OtherFoo

<强>变化:

1)将分类法名称移动到变量(在echo&#39;中使用)。

global $post;
$taxonomy = "property-feature";

// determine the topmost parent of a term
function get_term_top_most_parent($term_id, $taxonomy){
    // start from the current term
    $parent  = get_term_by( 'id', $term_id, $taxonomy);
    // climb up the hierarchy until we reach a term with parent = '0'
    while ($parent->parent != '0'){
        $term_id = $parent->parent;

        $parent  = get_term_by( 'id', $term_id, $taxonomy);
    }
    return $parent;
}

// so once you have this function you can just loop over the results returned by wp_get_object_terms

function hey_top_parents($taxonomy, $results = 1) {
    // get terms for current post
    $terms = wp_get_object_terms( get_the_ID(), $taxonomy );

    // set vars
    $top_parent_terms = array();
    foreach ( $terms as $term ) {
        //get top level parent
        $top_parent = get_term_top_most_parent( $term->term_id, $taxonomy );
        //check if you have it in your array to only add it once
        if ( !in_array( $top_parent, $top_parent_terms ) ) {
            $top_parent_terms[] = $top_parent;
        }
    }
    // build output (the HTML is up to you)
    foreach( $top_parent_terms as $single_feature ) 
    {
        echo '<li><a href="' . get_term_link( $single_feature->slug, $taxonomy ) .'">'. $single_feature->name . '</a></li>';

        foreach( $terms as $single ) {
            if( $single_feature->term_id == $single->parent ) {
                echo '<ul><li><a href="' . get_term_link( $single->slug, $taxonomy ) .'">'. $single->name . '</a></li></ul>';
            }
        }
    }

    //return $top_parent_terms;
}

2)将条件添加到负责显示子项的代码中。

$taxonomy = "property-feature";

删除(未使用的代码行):

1)

    if( $single_feature->term_id == $single->parent ) {
        echo '<ul><li><a href="' . get_term_link( $single->slug, $taxonomy ) .'">'. $single->name . '</a></li></ul>';
    }

2)

$features = get_the_terms( $post->ID, 'property-feature' );
$featuresss = get_the_terms( $post->ID, 'property-feature' );

<强>考虑:

1)从:

中删除$ results变量
$y = get_the_terms($terms->term_id, $taxonomy);

它不会在任何地方使用,或者可能应该确定是否应该由函数返回或显示结果。