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選択トップ親に表示するようになりまし必要があるということですその親から分類
$ taxonomy = "property-feature"を使用します。 argとして関数hey_top_parentsを呼び出すとき – Landon
この関数を使用するにはどうすればよいですか? –
ちょうどthxの使い方を見つけました –