0

I'm trying to display a list of parent and children custom taxonomy and under each the title of any post associated with the taxonomy.


What I have now is:

Category 1

Title

Title

Title

Category 2

Title

Category 3

Title

Title


What I want is:

Category 1

Sub category 1

Title

Sub category 2

Title

Title

Category 2

Title

Category 3

Sub category 1

Title

Sub category 1

Title


I did some researches but couldn't find a good solution to my problem.

This is the code I have so far:

$custom_terms = get_terms_customorder( 'publication-type', array(
    'parent' => 0,
    'hide_empty' => false,
    'post_status' => array('publish', 'future'))
);

foreach($custom_terms as $custom_term) {
    $args = array(
        'post_type' => 'publication',
        'post_status' => array('publish', 'future'),
        'posts_per_page' => -1,
        'tax_query' => array(
            array(
                'taxonomy' => 'publication-type',
                'field' => 'slug',
                'terms' => $custom_term->slug,
                'include_children' => true,
                'operator' => 'IN'
            ),
        ),
     );

    $loop = new WP_Query($args);
    if($loop->have_posts()) {
        echo '<div id="'.$custom_term->slug.'" class=" pt-100"><h2>'.$custom_term->name.'</h2>';
            while($loop->have_posts()) : $loop->the_post(); ?>
                <div class="timeline--item-content">
                    <?php if($post->post_status == 'future') echo '<span class="forthcoming"><em>'. __('Forthcoming', 'chairepenale') .'</em></span>'; ?>
                       <a href="<?php the_permalink(); ?>" class="timeline--item-permalink"><?php the_title(); ?></a>
                </div>
           <?php endwhile;
        echo '</div>';
    }
}
2
  • Can you please include code of below file so i can give you full code. get_template_part( 'single-templates/content/content', get_post_type() ); Feb 12 '19 at 6:36
  • I've edited to include the content Feb 12 '19 at 6:48
1

Here is the code...It may help you to display layout as you wanted. Also Here I have used post type as product and taxonomy as product_cat so you must need to change that as your code. The layout looks like this http://prntscr.com/mjxvzm

<?php
$post_type = 'product';
$taxonomy = 'product_cat';
$terms = get_terms( array(
    'taxonomy' => $taxonomy,
    'hide_empty' => true,
    'parent' => 0,
));
if(! empty($terms) && !is_wp_error($terms)): ?>
    <ul>
        <?php foreach($terms as $term){ $parent_term_id = $term->term_id; $parent_term_slug = $term->slug; ?>
            <li><a href="<?php echo get_term_link($term->slug,$taxonomy); ?>"><h2><?php echo $term->name; ?></h2></a></li>
            <?php
            $args = array(
                'parent' => $parent_term_id,
                'hide_empty' => true,
            ); 
            $sub_cat_terms = get_terms($taxonomy, $args);
                if(! empty($sub_cat_terms) && !is_wp_error($sub_cat_terms)): ?>
                    <ul>
                        <?php foreach($sub_cat_terms as $sub_cat_term){ $sub_cat_term_link = get_term_link( $sub_cat_term ); $child_term_slug = $sub_cat_term->slug; ?>
                            <li><a href="<?php echo $sub_cat_term_link; ?>"><h3><?php echo $sub_cat_term->name; ?></h3></a></li>
                            <?php
                                $args = array(
                                    'post_type' => $post_type,
                                    'post_status' => array('publish'),
                                    'posts_per_page' => -1,
                                    'tax_query' => array(
                                        array(
                                            'taxonomy' => $taxonomy,
                                            'field' => 'slug',
                                            'terms' => $child_term_slug,
                                        ),
                                    ),
                                 );

                                $loop = new WP_Query($args);
                                if($loop->have_posts()) { ?>
                                    <ul>
                                        <?php while($loop->have_posts()) : $loop->the_post(); ?>
                                            <li><a href="<?php the_permalink(); ?>"><h4><?php the_title(); ?></h4></a></li>
                                       <?php endwhile; ?>
                                    </ul>
                                <?php } ?> 
                        <?php } ?>
                    </ul>
            <?php else: ?>
                    <?php $args = array(
                        'post_type' => $post_type,
                        'post_status' => array('publish'),
                        'posts_per_page' => -1,
                        'tax_query' => array(
                            array(
                                'taxonomy' => $taxonomy,
                                'field' => 'slug',
                                'terms' => $parent_term_slug,
                            ),
                        ),
                     );

                    $loop = new WP_Query($args);
                    if($loop->have_posts()) { ?>
                        <ul>
                            <?php while($loop->have_posts()) : $loop->the_post(); ?>
                                <li><a href="<?php the_permalink(); ?>"><h4><?php the_title(); ?></h4></a></li>
                           <?php endwhile; ?>
                        </ul>
                    <?php } ?>
            <?php endif;?>
        <?php } ?>
    </ul>
<?php endif;?>
2
  • Well I kind of understand this code, but I'm trying to make it "fit" my structure and it doesn't work at all... Feb 12 '19 at 21:33
  • Let me know what is the problem you have been facing using my code. so I can clear understand. Feb 13 '19 at 6: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.