2016-07-21 4 views
3

選択したタクソノミーの親をサイトに表示したいとします。そのために私は上記のコードを持っていますが、この問題は選択されたタクソノミーを現在の構造のリストにすべて表示していることです。選択したタクソノミーのタクソノミーの親を表示する方法

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

Iを選択した場合、オプション2およびオプション3 I見たいと思う

親:オプション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; 

} 

私は、選択した分類のためのトップの親を表示するために管理が、今の問題は、私はanly選択トップ親に表示するようになりまし必要があるということですその親から分類

答えて

3
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; 
} 

CHANGES:

1)分類名を変数に移動しました(エコーで使用)。

$taxonomy = "property-feature"; 

2)サブ用語を表示するコードに条件が追加されている場合。

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

REMOVED (コードのラインを使用していない):

1)

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

2)

$y = get_the_terms($terms->term_id, $taxonomy); 

考慮してください

$結果変数を削除する0

1):それはどこかまたは多分使用されていない

function hey_top_parents($taxonomy, $results = 1) { 

は、結果が関数によって返さまたは表示されるべきかどうかを判断する必要があります。

+0

$ taxonomy = "property-feature"を使用します。 argとして関数hey_top_parentsを呼び出すとき – Landon

+0

この関数を使用するにはどうすればよいですか? –

+0

ちょうどthxの使い方を見つけました –

関連する問題