Podcast #128: We chat with Kent C Dodds about why he loves React and discuss what life was like in the dark days before Git. Listen now.
3 tried to make the code readable
source | link

As I was looking for the same problembut to get terms of one post, finally I compiled this, and it works for me. What

What it does :   
• it gets all terms of a taxonomy name for a specific post. 
• for a hierachical taxonomy with two levels (ex: level1:'country' and level2:'cities'), it createcreates a h4 with the level1 followed by an ul list of level2 and this for all level1 items. 
• if the taxonomy is not hierarchical, it will create only an ul list of all items. here is the code (I write it for me so I tried to be as generic as I can but...) : function finishingLister($heTerm){ $myterm = $heTerm; $terms = get_the_terms($post->ID,$myterm); if($terms){ $count = count($terms); echo '

'.$myterm; echo ((($count>1)&&(!endswith($myterm, 's')))?'s':"").'

';echo '';foreach($terms as $term){if(0 == $term->parent) $parentsItems[] = $term;if($term->parent) $childItems[] = $term;};if(is_taxonomy_hierarchical( $heTerm)){foreach($parentsItems as $parentsItem){echo ''.$parentsItem->name.'';echo '
    '; foreach($childItems as $childItem){ if ($childItem->parent == $parentsItem->term_id){ $termid = $myterm.'_' . ($childItem->term_id); $termIllu = get_field('tax_image',$termid); echo '
  • '.$parentsItem->name.'
  • '; }; }; echo '
';};}else{echo '
    '; foreach($parentsItems as $parentsItem){ $termid = $myterm.'_' . ($parentsItem->term_id); $termIllu = get_field('tax_image',$termid); echo '
  • '.$parentsItem->name.'
  • '; }; echo '
';};echo '';};};


function finishingLister($heTerm){
    $myterm = $heTerm;
    $terms = get_the_terms($post->ID,$myterm);
    if($terms){
        $count = count($terms);
        echo '<h3>'.$myterm;
        echo ((($count>1)&&(!endswith($myterm, 's')))?'s':"").'</h3>';
        echo '<div class="'.$myterm.'Wrapper">';
        foreach ($terms as $term) {
            if (0 == $term->parent) $parentsItems[] = $term;
            if ($term->parent) $childItems[] = $term; 
        };
        if(is_taxonomy_hierarchical( $heTerm )){
            foreach ($parentsItems as $parentsItem){
                echo '<h4>'.$parentsItem->name.'</h4>';
                echo '<ul>';
                foreach($childItems as $childItem){
                    if ($childItem->parent == $parentsItem->term_id){
                        echo '<li>'.$childItem->name.'</li>';
                    };
                };
                echo '</ul>';
            };
        }else{
            echo '<ul>';
            foreach($parentsItems as $parentsItem){
                echo '<li>'.$parentsItem->name.'</li>';
            };
            echo '</ul>';
        };
        echo '</div>';
    };
};

As I was looking for the same problem, finally I compiled this, and it works for me. What it does :  • it gets all terms of a taxonomy name for a specific post. • for a hierachical taxonomy with two levels (ex: level1:'country' and level2:'cities'), it create a h4 with the level1 followed by an ul list of level2 and this for all level1 items. • if the taxonomy is not hierarchical, it will create only an ul list of all items. here is the code (I write it for me so I tried to be as generic as I can but...) : function finishingLister($heTerm){ $myterm = $heTerm; $terms = get_the_terms($post->ID,$myterm); if($terms){ $count = count($terms); echo '

'.$myterm; echo ((($count>1)&&(!endswith($myterm, 's')))?'s':"").'

';echo '';foreach($terms as $term){if(0 == $term->parent) $parentsItems[] = $term;if($term->parent) $childItems[] = $term;};if(is_taxonomy_hierarchical( $heTerm)){foreach($parentsItems as $parentsItem){echo ''.$parentsItem->name.'';echo '
    '; foreach($childItems as $childItem){ if ($childItem->parent == $parentsItem->term_id){ $termid = $myterm.'_' . ($childItem->term_id); $termIllu = get_field('tax_image',$termid); echo '
  • '.$parentsItem->name.'
  • '; }; }; echo '
';};}else{echo '
    '; foreach($parentsItems as $parentsItem){ $termid = $myterm.'_' . ($parentsItem->term_id); $termIllu = get_field('tax_image',$termid); echo '
  • '.$parentsItem->name.'
  • '; }; echo '
';};echo '';};};

As I was looking for the same but to get terms of one post, finally I compiled this, and it works for me.

What it does :  
• it gets all terms of a taxonomy name for a specific post. 
• for a hierachical taxonomy with two levels (ex: level1:'country' and level2:'cities'), it creates a h4 with the level1 followed by an ul list of level2 and this for all level1 items. 
• if the taxonomy is not hierarchical, it will create only an ul list of all items. here is the code (I write it for me so I tried to be as generic as I can but...) :

function finishingLister($heTerm){
    $myterm = $heTerm;
    $terms = get_the_terms($post->ID,$myterm);
    if($terms){
        $count = count($terms);
        echo '<h3>'.$myterm;
        echo ((($count>1)&&(!endswith($myterm, 's')))?'s':"").'</h3>';
        echo '<div class="'.$myterm.'Wrapper">';
        foreach ($terms as $term) {
            if (0 == $term->parent) $parentsItems[] = $term;
            if ($term->parent) $childItems[] = $term; 
        };
        if(is_taxonomy_hierarchical( $heTerm )){
            foreach ($parentsItems as $parentsItem){
                echo '<h4>'.$parentsItem->name.'</h4>';
                echo '<ul>';
                foreach($childItems as $childItem){
                    if ($childItem->parent == $parentsItem->term_id){
                        echo '<li>'.$childItem->name.'</li>';
                    };
                };
                echo '</ul>';
            };
        }else{
            echo '<ul>';
            foreach($parentsItems as $parentsItem){
                echo '<li>'.$parentsItem->name.'</li>';
            };
            echo '</ul>';
        };
        echo '</div>';
    };
};
2 tried to make the code readable
source | link

As I was looking for the same problem, finally I compiled this, and it works for me. What it does : • it gets all terms of a taxonomy name for a specific post. • for a hierachical taxonomy with two levels (ex: level1:'country' and level2:'cities'), it create a h4 with the level1 followed by an ul list of level2 and this for all level1 items. • if the taxonomy is not hierarchical, it will create only an ul list of all items. here is the code (I write it for me so I tried to be as generic as I can but...) : function finishingLister($heTerm){    $myterm = $heTerm;    $terms = get_the_terms($post->ID,$myterm);    if($terms){    $count = count($terms);    echo '

'.$myterm;    echo ((($count>1)&&(!endswith($myterm, 's')))?'s':"").'

';    echo '';    foreach ($terms as $term) {    if (0 == $term->parent) $parentsItems[] = $term;    if ($term->parent) $childItems[] = $term;    };    if(is_taxonomy_hierarchical( $heTerm )){    foreach ($parentsItems as $parentsItem){    echo ''.$parentsItem->name.'';    echo '
    ';    foreach($childItems as $childItem){    if ($childItem->parent == $parentsItem->term_id){    $termid = $myterm.'_' . ($childItem->term_id);    $termIllu = get_field('tax_image',$termid);    echo '
  • '.$parentsItem->name.'
  • ';    };    };    echo '
';    };    }else{    echo '
    ';    foreach($parentsItems as $parentsItem){    $termid = $myterm.'_' . ($parentsItem->term_id);    $termIllu = get_field('tax_image',$termid);    echo '
  • '.$parentsItem->name.'
  • ';    };    echo '
';    };    echo '';    }; };

So finally you call the function with this (obviously, you replace my_taxonomy by yours) : finishingLister('my_taxonomy');

I don't pretend it's perfect but as I said it works for me.

As I was looking for the same problem, finally I compiled this, and it works for me. What it does : • it gets all terms of a taxonomy name for a specific post. • for a hierachical taxonomy with two levels (ex: level1:'country' and level2:'cities'), it create a h4 with the level1 followed by an ul list of level2 and this for all level1 items. • if the taxonomy is not hierarchical, it will create only an ul list of all items. here is the code (I write it for me so I tried to be as generic as I can but...) : function finishingLister($heTerm){  $myterm = $heTerm;  $terms = get_the_terms($post->ID,$myterm);  if($terms){  $count = count($terms);  echo '

'.$myterm;  echo ((($count>1)&&(!endswith($myterm, 's')))?'s':"").'

';  echo '';  foreach ($terms as $term) {  if (0 == $term->parent) $parentsItems[] = $term;  if ($term->parent) $childItems[] = $term;  };  if(is_taxonomy_hierarchical( $heTerm )){  foreach ($parentsItems as $parentsItem){  echo ''.$parentsItem->name.'';  echo '
    ';  foreach($childItems as $childItem){  if ($childItem->parent == $parentsItem->term_id){  $termid = $myterm.'_' . ($childItem->term_id);  $termIllu = get_field('tax_image',$termid);  echo '
  • '.$parentsItem->name.'
  • ';  };  };  echo '
';  };  }else{  echo '
    ';  foreach($parentsItems as $parentsItem){  $termid = $myterm.'_' . ($parentsItem->term_id);  $termIllu = get_field('tax_image',$termid);  echo '
  • '.$parentsItem->name.'
  • ';  };  echo '
';  };  echo '';  }; };

So finally you call the function with this (obviously, you replace my_taxonomy by yours) : finishingLister('my_taxonomy');

I don't pretend it's perfect but as I said it works for me.

As I was looking for the same problem, finally I compiled this, and it works for me. What it does : • it gets all terms of a taxonomy name for a specific post. • for a hierachical taxonomy with two levels (ex: level1:'country' and level2:'cities'), it create a h4 with the level1 followed by an ul list of level2 and this for all level1 items. • if the taxonomy is not hierarchical, it will create only an ul list of all items. here is the code (I write it for me so I tried to be as generic as I can but...) : function finishingLister($heTerm){  $myterm = $heTerm;  $terms = get_the_terms($post->ID,$myterm);  if($terms){  $count = count($terms);  echo '

'.$myterm;  echo ((($count>1)&&(!endswith($myterm, 's')))?'s':"").'

';  echo '';  foreach ($terms as $term) {  if (0 == $term->parent) $parentsItems[] = $term;  if ($term->parent) $childItems[] = $term;   };  if(is_taxonomy_hierarchical( $heTerm )){  foreach ($parentsItems as $parentsItem){  echo ''.$parentsItem->name.'';  echo '
    ';  foreach($childItems as $childItem){  if ($childItem->parent == $parentsItem->term_id){  $termid = $myterm.'_' . ($childItem->term_id);  $termIllu = get_field('tax_image',$termid);  echo '
  • '.$parentsItem->name.'
  • ';  };  };  echo '
';  };  }else{  echo '
    ';  foreach($parentsItems as $parentsItem){  $termid = $myterm.'_' . ($parentsItem->term_id);  $termIllu = get_field('tax_image',$termid);  echo '
  • '.$parentsItem->name.'
  • ';  };  echo '
';  };  echo '';  }; };

So finally you call the function with this (obviously, you replace my_taxonomy by yours) : finishingLister('my_taxonomy');

I don't pretend it's perfect but as I said it works for me.

1
source | link

As I was looking for the same problem, finally I compiled this, and it works for me. What it does : • it gets all terms of a taxonomy name for a specific post. • for a hierachical taxonomy with two levels (ex: level1:'country' and level2:'cities'), it create a h4 with the level1 followed by an ul list of level2 and this for all level1 items. • if the taxonomy is not hierarchical, it will create only an ul list of all items. here is the code (I write it for me so I tried to be as generic as I can but...) : function finishingLister($heTerm){ $myterm = $heTerm; $terms = get_the_terms($post->ID,$myterm); if($terms){ $count = count($terms); echo '

'.$myterm; echo ((($count>1)&&(!endswith($myterm, 's')))?'s':"").'

'; echo ''; foreach ($terms as $term) { if (0 == $term->parent) $parentsItems[] = $term; if ($term->parent) $childItems[] = $term; }; if(is_taxonomy_hierarchical( $heTerm )){ foreach ($parentsItems as $parentsItem){ echo ''.$parentsItem->name.''; echo '
    '; foreach($childItems as $childItem){ if ($childItem->parent == $parentsItem->term_id){ $termid = $myterm.'_' . ($childItem->term_id); $termIllu = get_field('tax_image',$termid); echo '
  • '.$parentsItem->name.'
  • '; }; }; echo '
'; }; }else{ echo '
    '; foreach($parentsItems as $parentsItem){ $termid = $myterm.'_' . ($parentsItem->term_id); $termIllu = get_field('tax_image',$termid); echo '
  • '.$parentsItem->name.'
  • '; }; echo '
'; }; echo ''; }; };

So finally you call the function with this (obviously, you replace my_taxonomy by yours) : finishingLister('my_taxonomy');

I don't pretend it's perfect but as I said it works for me.