1

I have a list of custom taxonomy IDs like (1, 2, 3, 10). I want to display a list of these specified categories by names and each should have its childs like:

Europe

  • France
  • Germany

Africa

  • Nigeria
  • South Africa

I use this one and somehow it works, but it shows only the child categories without the parent category, also what's important is to allow to put a list of IDs instead of only one ID (10 in the example)

$term_id = 10;
$taxonomy_name = 'products';
$termchildren = get_term_children( $term_id, $taxonomy_name );

echo '<ul>';
foreach ( $termchildren as $child ) {
    $term = get_term_by( 'id', $child, $taxonomy_name );
    echo '<li><a href="' . get_term_link( $child, $taxonomy_name ) . '">' . $term->name . '</a></li>';
}
echo '</ul>';
1

You're already on the right way; just add another loop for the $parent_terms array around your existing loop. Also it might be a good idea to check if the numbers in the array are valid term IDs, so you might end up doing something like this:

$parent_terms = array( 1, 2, 3, 10 );
$taxonomy = 'products';

echo '<ul>';
foreach ( $parent_terms as $parent_term_id ) {
  $parent_term = get_term_by( 'id', $parent_term_id, $taxonomy );

  if ( $parent_term ) {
    echo '<li><a href="' . get_term_link( $parent_term_id, $taxonomy ) . '">' . $parent_term->name . '</a>';

    $child_terms = get_term_children( $parent_term_id, $taxonomy );
    if ( $child_terms ) {
      echo '<ul>';
      foreach ( $child_terms as $child_term_id ) {
        $child_term = get_term_by( 'id', $child_term_id, $taxonomy );
        echo '<li><a href="' . get_term_link( $child_term_id, $taxonomy ) . '">' . $child_term->name . '</a></li>';
      } // end of child terms loop
      echo '</ul>';
    } // end of $child_terms

  echo '</li>';
  } // end of $parent_term

} // end of parent terms loop
echo '</ul>';

Update: Assuming you're using something like OptionTree plugin you can populate the $parent_terms array like this:

$parent_terms = array(); // define empty array
$term_options = ot_get_option('option-name');
foreach ( $term_options as $term_option_id ) {
  // convert strings to numbers and save them in $parent_terms
  array_push( $parent_terms, intval($term_option_id) );
}
  • Thank YOU SOO MUCH Sven for the help, It did the trick. but unfortunately, I use an option to rectrive the categories ID, using the implode(', ', get_option('option-name')) which is displaying that list of ID.. can you please be more generous and tell me how to integrate that array of ID came from the option in the code, much appreciated – dzdesigner Dec 5 '14 at 12:25
  • Sorry I edited recent comment instead of adding a new one. either you help me with the option one or not, I'll mark it as resolved as you answered the question perfectly. – dzdesigner Dec 5 '14 at 12:30
  • @dzdesigner What is the output of var_dump(get_option('option-name'))? You can try to assign it like this: $parent_terms = get_option('option-name'); – Sven Dec 5 '14 at 12:32
  • I tried that but it returns an error Object of class WP_Error could not be converted to string ..the option is multiselect to pick the list of the categories and outputs them by IDs – dzdesigner Dec 5 '14 at 12:37
  • 1
    I LOVE YOUUUU MAN :D, you saved my day, really, thank You sooo much, it worked perfectly. too bad I have not enough reputation to upvote it, but you are amazing. THANK YOU SO MUCH – dzdesigner Dec 5 '14 at 14:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.