1

I'd like to display only first level children of my custom taxonomy categories I'am using wp 3.8

I've something like for my taxonomy "listing_category": Category 1 SubCattegoy1 Subsub category1

I'd like to display only a link to "sub category1" when I'am on "Category 1" (not "subsub category1"). With the code below It displays all sub cat and subsub cat :

$taxonomy_name = 'listing_category';
$queried_object = get_queried_object();
$term_id = $queried_object->term_id;
$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>';

Any ideas to help me ? Thanks

  • Have you searched the site? There should be plenty of similar questions available. – kaiser Feb 25 '14 at 21:01
2

Try the following code:

$taxonomy_name = 'listing_category';
$queried_object = get_queried_object();
$term_id = $queried_object->term_id;

$termchildren = get_terms( $taxonomy_name, array( 'parent' => $term_id, 'hide_empty' => false ) );

echo '<ul>';
   foreach ( $termchildren as $child ) {
       echo '<li><a href="' . get_term_link( $child, $taxonomy_name ) . '">' . $child->name . '</a></li>';
   }
echo '</ul>';
| improve this answer | |
  • Ok, @1fixdotio, many thanks, It is almost perfect ;-) – Nicolas Feb 26 '14 at 7:29
  • Your code list only the first item of the subcategory. In fact (and I think my question was not clear, sorry), I try to list all the subcategories just below the current cat, and just one level below. Any ideas ? My english is not completly fluent ;-) – Nicolas Feb 26 '14 at 7:36
  • Sooory ! It displays only subcategories which contains an item ;-) – Nicolas Feb 26 '14 at 7:44
  • Just updated my code. Set hide_empty to false, and it will list all sub categories even it contains no post. Let me know I still misunderstood your question. – 1fixdotio Feb 26 '14 at 8:14
  • Glad it helped. Could you mark my answer as "accepted"? Thanks. – 1fixdotio Feb 26 '14 at 8:22

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.