同时显示父分类和子自定义分类/内容

时间:2019-02-12 作者:Sway Raines

我试图显示一个父级和子级自定义分类法的列表,并在每个列表下显示与该分类法关联的任何帖子的标题。

我现在拥有的是:

类别1标题

标题

标题

类别2标题

类别3标题

标题

我想要的是:

第1类第1子类标题

子类别2标题

标题

类别2标题

第3类第1子类标题

子类别1标题

我做了一些研究,但没有找到解决问题的好办法。

这是我目前掌握的代码:

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

1 个回复
SO网友:Tanmay Patel

这是密码。。。它可以帮助您根据需要显示布局。这里我也用过post type as producttaxonomy as product_cat 因此,您必须将其更改为您的代码。布局如下所示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;?>

相关推荐