WordPress.org

Ready to get started?Download WordPress

Forums

display hierarchical taxonomy list (5 posts)

  1. robertodimarco
    Member
    Posted 1 year ago #

    Hello! I need to display my hierarchical custom taxonomy like this:

    Parent
    -child
    -child
    -child
    Parent
    -child
    -child

    I used echo get_the_term_list( $post->ID, 'my-taxonomy' );
    but it doesn't display hierarchy. Any helps? Thanks!

  2. keesiemeijer
    moderator
    Posted 1 year ago #

    Try it with this:

    <?php
    $taxonomy = 'category'; // change this to your taxonomy
    $terms = wp_get_post_terms( $post->ID, $taxonomy, array( "fields" => "ids" ) );
    if( $terms ) {
      echo '<ul>';
    
      $terms = trim( implode( ',', (array) $terms ), ' ,' );
      wp_list_categories( 'title_li=&taxonomy=' . $taxonomy . '&include=' . $terms );
    
      echo '</ul>';
    }
    ?>
  3. robertodimarco
    Member
    Posted 1 year ago #

    That's great!!! Thank you so much!!!
    Now, i need to get image associated with terms. In my old code, i used this :

    <img src="<?php echo z_taxonomy_image_url($cat->term_id); ?>/>

    with the plugin "Categories Images".
    How can create a custom list like:

    Parent
    -child -image
    -child -image
    -child -image

    I suppose i need to attach code between the
    <li></li> elements inside wp_list_categories, isn't it?

  4. keesiemeijer
    moderator
    Posted 1 year ago #

    Can you show the old code and output? Was the image inside the term link or outside it? Do parent terms have a image?

  5. robertodimarco
    Member
    Posted 1 year ago #

    in my old code i echoed terms and image. I can associate image and terms with the plugin "Category Images" :

    <ul>
    <?php foreach (wp_get_post_terms($post->ID,'my-taxonomy') as $cat) : ?>
    <li  style="display:inline-block;">
    <img src="<?php echo z_taxonomy_image_url($cat->term_id); ?>/>
    <p><?php echo $cat->name; ?></p>
    </li>
    <?php endforeach; ?>
    </ul>

    Parents (in this case "Colori") don't need image associated, but it's not a problem. The output :
    http://cl.ly/image/1q1G2z2c3K1J

Topic Closed

This topic has been closed to new replies.

About this Topic