如何在循环中显示自定义分类法的子项?

问题描述

我为帖子、主题创建了一个分类

add_action( 'init','create_cw_hierarchical_taxonomy',0 );
//create a custom taxonomy name
function create_cw_hierarchical_taxonomy() {
$labels = array(
'name' => _x( 'Topics','taxonomy general name' ),'singular_name' => _x( 'Topic','taxonomy singular name' ),'search_items' => __( 'Search Topics' ),'all_items' => __( 'All Topics' ),'parent_item' => __( 'Parent Topic' ),'parent_item_colon' => __( 'Parent Topic:' ),'edit_item' => __( 'Edit Topic' ),'update_item' => __( 'Update Topic' ),'add_new_item' => __( 'Add New Topic' ),'new_item_name' => __( 'New Topic Name' ),'menu_name' => __( 'Topics' ),);
// taxonomy register
register_taxonomy('topics',array('post'),array(
'hierarchical' => true,'labels' => $labels,'show_ui' => true,'show_admin_column' => true,'query_var' => true,'rewrite' => array( 'slug' => 'topic' ),));
}

现在我在后台创建了两个主题主题 1 和主题 2

我想在不同的循环中显示这两个主题,所以我编写了以下代码

<?PHP
            $options = array(
                'post_type' => 'post','posts_per_page' => 1,'orderby' => 'date','order' => 'ASC','paged' => $paged,'tax_query' => array(
                    array(
                        'taxonomy' => 'topics','field' => 'name','terms' => 'topic-1'
                    )
                )
            ); ?>
            
            <?PHP $query = new WP_Query( $options );
            if ( $query->have_posts() ) :
                while($query->have_posts()) : $query->the_post();?> 

            <div class="col-sm-8">
                <div class="featured-post-large m-m-b-1">
                    <div class="featured-post-large-control text-light">
                        <div class="featured-post-large-content mx-5">
                            <div><?PHP the_category( $separator,$parents,$post_id ); ?></div>
                            <h2><a href="<?PHP echo get_permalink($post->ID) ?>" class="archive-title"><?PHP the_title()?></a></h2>
                            <div class="py-2"><?PHP the_excerpt()?></div>
                        </div>
                    </div>
                </div>
            </div>
            
            <?PHP endwhile; wp_reset_query();
            endif; ?>

我无法获得主题 1 和主题 2 下的帖子,请指导我。谢谢!

解决方法

$options = array(
                'post_type' => 'post','posts_per_page' => 1,'orderby' => 'date','order' => 'ASC','paged' => $paged,'tax_query' => array(
                    array(
                        'taxonomy' => 'topics','field' => 'slug','terms' => 'topic-1'
                    )
                )
            );