i want show on site parent of selected taxonomy. have above code problem is showing selected taxonomy in list structure.
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 $single_feature ) { echo '<li><a href="' . get_term_link( $single_feature->slug, 'property-feature' ) .'">'. $single_feature->name . '</a></li>'; } ?> </ul> </div> <?php
}
example:
parent: option 1, option 2
parent 2 : option 3, option 4
if select option 2 , option 3 want see
parent: option 2
parent 2 : option 3
updare 2
global $post; $features = get_the_terms( $post->id, 'property-feature' ); $featuresss = get_the_terms( $post->id, 'property-feature' ); // determine topmost parent of term function get_term_top_most_parent($term_id, $taxonomy){ // start current term $parent = get_term_by( 'id', $term_id, $taxonomy); // climb hierarchy until reach term parent = '0' while ($parent->parent != '0'){ $term_id = $parent->parent; $parent = get_term_by( 'id', $term_id, $taxonomy); } return $parent; } // once have function can loop on results returned wp_get_object_terms function hey_top_parents($taxonomy, $results = 1) { // terms 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 $term ) { //get top level parent $top_parent = get_term_top_most_parent( $term->term_id, $taxonomy ); //check if have in array add once if ( !in_array( $top_parent, $top_parent_terms ) ) { $top_parent_terms[] = $top_parent; } } // build output (the html you) foreach( $top_parent_terms $single_feature ) { echo '<li><a href="' . get_term_link( $single_feature->slug, 'property-feature' ) .'">'. $single_feature->name . '</a></li>'; foreach( $terms $single ) { echo '<ul><li><a href="' . get_term_link( $single->slug, 'property-feature' ) .'">'. $single->name . '</a></li></ul>'; } } //return $top_parent_terms; }
i managed display top parent selected taxonomy problem need display in top parent anly selected taxonomy parent
global $post; $taxonomy = "property-feature"; // determine topmost parent of term function get_term_top_most_parent($term_id, $taxonomy){ // start current term $parent = get_term_by( 'id', $term_id, $taxonomy); // climb hierarchy until reach term parent = '0' while ($parent->parent != '0'){ $term_id = $parent->parent; $parent = get_term_by( 'id', $term_id, $taxonomy); } return $parent; } // once have function can loop on results returned wp_get_object_terms function hey_top_parents($taxonomy, $results = 1) { // terms current post $terms = wp_get_object_terms( get_the_id(), $taxonomy ); // set vars $top_parent_terms = array(); foreach ( $terms $term ) { //get top level parent $top_parent = get_term_top_most_parent( $term->term_id, $taxonomy ); //check if have in array add once if ( !in_array( $top_parent, $top_parent_terms ) ) { $top_parent_terms[] = $top_parent; } } // build output (the html you) foreach( $top_parent_terms $single_feature ) { echo '<li><a href="' . get_term_link( $single_feature->slug, $taxonomy ) .'">'. $single_feature->name . '</a></li>'; foreach( $terms $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) moved taxonomy name variable (used in echo's).
$taxonomy = "property-feature";
2) added if condition code responsible displaying sub terms.
if( $single_feature->term_id == $single->parent ) { echo '<ul><li><a href="' . get_term_link( $single->slug, $taxonomy ) .'">'. $single->name . '</a></li></ul>'; }
removed (not used lines of codes):
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);
consider:
1) removing $results variable from:
function hey_top_parents($taxonomy, $results = 1) {
it not used anywhere or maybe should determine if results should returned or displayed function.
Comments
Post a Comment