$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Display both parent and child custom taxonomycontent|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Display both parent and child custom taxonomycontent

matteradmin11PV0评论

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>';
    }
}

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>';
    }
}
Share Improve this question edited Jun 15, 2020 at 8:21 CommunityBot 1 asked Feb 12, 2019 at 4:59 Sway RainesSway Raines 114 bronze badges 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() ); – Tanmay Patel Commented Feb 12, 2019 at 6:36
  • I've edited to include the content – Sway Raines Commented Feb 12, 2019 at 6:48
Add a comment  | 

1 Answer 1

Reset to default 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/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;?>
Post a comment

comment list (0)

  1. No comments so far