• Resolved petedc

    (@petedc)


    Hi

    I have a CT of Staff Department and CP of Staff.

    I want to display all the posts listed by e.g:

    Parent Category
    – Child Category
    – Post Name
    – Post Name
    – Post Name
    – Child Category
    – Post Name
    – Post Name
    Parent Category
    – Child Category
    – Post Name
    – Post Name
    – Post Name
    – Child Category
    – Post Name
    – Post Name

    I have the following code which works fine but only displays the posts under the category it is listed in.

    function dc_show_staff(){
        $terms = get_terms( 'staff_department' );
    
        foreach ( $terms as $term ) {
            $term_name = $term->name;
    
            $args = array(
                    'orderby'   => 'title',
                    'order'     => 'ASC',
                    'post_per_page' => -1,
                    'hide_empty' => 0,
                    'tax_query'   => array(
                                    array(
                                      'taxonomy' => 'staff_department' ,
                                      'field' => 'slug',
                                      'terms' => $term->slug,
                                      'include_children' => 0
                                    )),
                    );       
    
            $term_posts = new WP_Query( $args );
    
            while( $term_posts->have_posts() ) {
                $term_posts->the_post();
    
                if ( $term_name ) {
                    $output .= '<h3>'.$term_name.'</h3><div class="row">';
                    $term_name = '';
                }
    
                $output .= '<div class="col-sm-2 staff-item">';
                $output .= '<a href="' . get_permalink() . '">' .get_the_post_thumbnail($post->ID , 'thumbnail', 'class=img img-circle img-responsive'). '</a>';
                $output .= '<a href="'.get_permalink().'">'.get_the_title().'</a>';
                $output .= get_post_meta($post->ID, 'job_title', true);
                $output .= '</div>';
            }
    
            if ( !$term_name ) {
              $output .= '</div>';
            }
        }
        wp_reset_postdata();
        wp_reset_query();
    
        return $output;
    }

    How would I modify this to display the Parent Category as a header for each child category? Thanks!

Viewing 2 replies - 1 through 2 (of 2 total)
  • Moderator bcworkz

    (@bcworkz)

    Add an additional loop for just parent categories. To get just a single level, provide a ‘parent’ argument to get_terms(). By supplying 0 as a ‘parent’ value, you will get just the top level terms.

    Then for each parent run the rest of your code, supplying the parent ID as the ‘parent’ value in get_terms(), which will return all immediate children of that parent.

    Since the parent term is the outermost loop, it’s data will be available for all of it’s children’s loops, making the display of the parent above each child simple.

    Thread Starter petedc

    (@petedc)

    Awesome, thanks for pointing me in the right direction! Final code if it helps anyone:

    function dc_show_staff(){
    
        $parent_terms = get_terms( 'staff_department', array( 'parent' => 0 ));
    
        foreach ( $parent_terms as $parent_term ) {
    
            $output .= '<h3>'.$parent_term->name.'</h3>';
    
            $child_terms = get_terms( 'staff_department', array( 'parent' => $parent_term->term_id ));
    
            foreach ( $child_terms as $child_term ) {
                $args = array(
                        'orderby'   => 'title',
                        'order'     => 'ASC',
                        'post_per_page' => -1,
                        'hide_empty' => 0,
                        'tax_query'   => array(
                                        array(
                                          'taxonomy' => 'staff_department' ,
                                          'field' => 'slug',
                                          'terms' => $child_term->slug,
                                          'include_children' => 1
                                        )),
                        );       
    
                $term_posts = new WP_Query( $args );
                $term_name = $child_term->name;
    
                while( $term_posts->have_posts() ) {
                    $term_posts->the_post();
    
                    if ( $term_name ) {
                        $output .= '<h3>'.$term_name.'</h3><div class="row">';
                        $term_name = '';
                    }
    
                    $output .= '<div class="col-sm-2 staff-item">';
                    $output .= '<a href="' . get_permalink() . '">' .get_the_post_thumbnail($post->ID , 'thumbnail', 'class=img img-circle img-responsive'). '</a>';
                    $output .= '<a href="'.get_permalink().'">'.get_the_title().'</a>';
                    $output .= get_post_meta($post->ID, 'job_title', true);
                    $output .= '</div>';
                }
    
                if ( !$term_name ) {
                  $output .= '</div>';
                }
            }
        }
        wp_reset_postdata();
        wp_reset_query();
    
        return $output;
    }
Viewing 2 replies - 1 through 2 (of 2 total)

The topic ‘Listing Custom Posts in Custom Taxonomy’ is closed to new replies.