I hope someone can help me, I am trying to get my custom post sub categories to display but can only seem to display one of them. Is there a way I can loop through them all? Im a front end developer and this PHP is messing my brain. (Dont know how you do it)

The function is below, any help would be great.

 * Get taxonomies terms links.
 * @see get_object_taxonomies()
function wpdocs_custom_taxonomies_terms_links() {
    // Get post by post ID.
    $post = get_post( $post->ID );

// Get post type by post.
$post_type = $post->post_type;

// Get post type taxonomies.
$taxonomies = get_object_taxonomies( $post_type, 'objects' );

$out = array();

foreach ( $taxonomies as $taxonomy_slug => $taxonomy ){

    // Get the terms related to post.
    $terms = get_the_terms( $post->ID, $taxonomy_slug );

    if ( ! empty( $terms ) ) {
        $out[] = "<h2>" . $taxonomy->label . "</h2>\n<ul>";
        foreach ( $terms as $term ) {
            $out[] = sprintf( '<li><a href="%1$s">%2$s</a></li>',
                esc_url( get_term_link( $term->slug, $taxonomy_slug ) ),
                esc_html( $term->name )
        $out[] = "\n</ul>\n";
return implode( '', $out );


enter image description here

The Sub categories that I expect to see are below - Alpha Pizza is the parent and the sub catgories are below. enter image description here

  • Are you trying to retrieve all terms for all taxonomies for the post type, or just those that are assigned to the particular post be viewed?
    – Dan.
    Nov 28 '16 at 15:35
  • 1
    Can you add to your question a list of what you expect to see as well?
    – socki03
    Nov 28 '16 at 15:35
  • I have amended to question to show which categories im looking for.that arent appearing. Nov 28 '16 at 15:44
  • @Dan im only looking to display the sub categories / posts of the current parent... in this case Alpha Pizza Nov 28 '16 at 15:45
  • I see. So the the new image in your question is showing a 'top-level' category, rather than a page?
    – Dan.
    Nov 28 '16 at 15:49

Assuming you want to show the terms of just one taxonomy at a time, then this will produce what you're looking for:

function my_theme_get_child_terms(){

    $return = '';
    $current_term = get_queried_object()->term_id;
    $term_info = get_term($current_term);
    $sub_categories = get_term_children( $current_term, $term_info->taxonomy );

    $return .= '<h2>' . $term_info->taxonomy . '</h2><ul>';
    foreach ( $sub_categories as $cat ) {
        $term = get_term_by( 'id', $cat, $term_info->taxonomy );
         $return .= sprintf( '<li><a href="%1$s">%2$s</a></li>',
            esc_url( get_term_link( $cat, $term_info->taxonomy ) ),
            esc_html( $term->name )
    $return .= '</ul>';

    return $return;

You need to put the above code inside a relevant template file. E.g. taxonomy.php or index.php.

  • Thanks for this Im not sure I understand where to put it? do i need to wrap it in something? i need to show all of the sub categories for each parent not just one... sorry if i confused you, thanks for your help Nov 28 '16 at 16:13
  • Edited just now to make it a bit easier. Put the above function into your theme's functions.php (replacing 'my_theme' with your theme's slug) and then in 'category-ranges-template.php', replace 'wpdocs_custom_taxonomies_terms_links()' with 'my_theme_get_child_terms()'
    – Dan.
    Nov 28 '16 at 16:17

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.