1

I'm trying to show both the parent and child categories of my custom post type separated by the first letter of the category

What I have.

A.
Apples

B. 
Bananas

C. 
Coconuts

What I want

A.
Apples<br />
-Red<br />
-Green<br />

B. 
Bananas

C. 
Coconuts

Here's the code I have now. I just need it to show the child categories too. Thanks in advanced.

<?php 
$list = '';
$args = array(
    'hide_empty' => true,  // Set this to true to hide terms with no posts
    'hierarchical' => true,
    'pad_counts' => false 
);
$tags = get_terms('type',$args);
$groups = array(  );

if( $tags && is_array( $tags ) ) {
    foreach( $tags as $tag ) {
        $first_letter = strtoupper( $tag->name[0] );
        $groups[ $first_letter ][] = $tag;
}
if( !empty( $groups ) ) {
    $index_line = '';
    foreach( $groups as $letter => $tags ) {
        $list .= '<h4><p id="' . apply_filters( 'the_title', $letter ) . '"><strong>'. apply_filters( 'the_title', $letter ) .'</strong></P></h4><hr><ul>';
    foreach( $tags as $tag ) {
        $name = apply_filters( 'the_title', $tag->name );
        $list .= '<li><a href="' . get_term_link( $tag ) . '" title="' . sprintf(__('View all posts tagged with %s', ''), $tag->name) . '">' . $tag->name . ' </a></li>';
  }
  $list .= '</ul>'; 
}
$list .= '';
}
} else $list .= '<p>Sorry, but no tags were found</p>';

print ($index_line);
print ($list);
0

You should simply be able to add another foreach loop, using the get_term_children() function.

Change your lines:

foreach( $tags as $tag ) {
    $name = apply_filters( 'the_title', $tag->name );
    $list .= '<li><a href="' . get_term_link( $tag ) . '" title="' . sprintf(__('View all posts tagged with %s', ''), $tag->name) . '">' . $tag->name . ' </a></li>';
}

...to...

foreach( $tags as $tag ) {
    $name = apply_filters( 'the_title', $tag->name );
    $list .= '<li><a href="' . get_term_link( $tag ) . '" title="' . sprintf(__('View all posts tagged with %s', ''), $tag->name) . '">' . $tag->name . ' </a>';

    // show children in their own ul
    if($children = get_term_children($tag->ID, 'type')) {
        $list .= '<ul>';
        foreach($children as $child) {
            $list .= '<li><a href="' . get_term_link( $child ) . '" title="' . sprintf(__('View all posts tagged with %s', ''), $child->name) . '">' . $tag->name . ' </a></li>';
        }
        $list .= '</ul>';
    }

    // close li after this one
    $list .= '</li>';
}

(untested)

You could make this method far more beautiful if you would do it recursive ;) But I think this should do.

  • Hmm, it isn't working. I think that there's some other issue going on. – lievo4 Nov 9 '15 at 14:04
  • does it throw an error or something? Ist "type" the taxonomy name, and is it hierarchical? – simonthesorcerer Nov 10 '15 at 16:05

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.