I have defined a custom post type for creating galleries using WP built in gallery functionality.

register_post_type( 'my_gallery',
        'labels' => array(
            'name' => __( 'Gallery', 'my-child-theme' ),
            'singular_name' => __( 'Gallery', 'my-child-theme' )
        'public' => true,
        'supports' => array( 'title', 'editor', 'custom-fields', 'thumbnail' )

Also created custom taxonomy for this specific CPT, with a main category 'Galleries', and a subcategory 'History':


I also have a template in child theme called taxonomy-galleries.php which works as it should, it displays all the posts in galleries, and in history too.

Now what I want is this: Display all the posts in main category, and display the subcategory history, paged with max 12 elements.

How would I do that?

Edit based on @mmm -s answer how to get all the elements in one array:

    $all_the_items = array();
    while ( have_posts() ) : the_post();
          $t = get_the_terms( $post->ID , 'my-taxonomy' );
                if ($t[0]->parent == 0){
                $properties = array(
                    'title' => get_the_title(),
                    'link' => get_the_permalink() );
                 $all_the_items[] = $properties;
endwhile; wp_reset_query();
    $o = get_queried_object();
            $children = get_term_children($o->term_id, $o->taxonomy);
            foreach ($children as $child) {
                $this_term = get_term($child);
                $properties = array(
                    'title' => $this_term->name,
    'link' => site_url('/' . $this_term->taxonomy . '/' . $this_term->slug . '/')
  • you want to display the subcategories on the same page ? – mmm Jan 3 '18 at 11:37
  • Yes I want the items from the root category and subcategories (without the items, items in the subcategories will have a separate page for them). – D. Dan Jan 3 '18 at 11:44

to get the children of the queried object, you can use this code is the file taxonomy-galleries.php

$o = get_queried_object();
$children = get_term_children($o->term_id, $o->taxonomy);

$children is an array of term identifiers then you just need to loop over this.

| improve this answer | |

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.