如何解决如何在循环中显示自定义分类法的子项?
我为帖子、主题创建了一个分类法
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'
)
)
);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。