显示分类中的所有层次化帖子类型

时间:2017-10-03 作者:lucbord

我有这个循环,效果很好,但cpt Studio是分层的,我只想在循环中显示第一级帖子。。我找不到解决方案。。。

<?php

$post_type = \'studio\';

// Get all the taxonomies for this post type


   $taxonomies = get_object_taxonomies( array( \'post_type\' => $post_type ) );


foreach( $taxonomies as $taxonomy ) :


// Gets every "category" (term) in this taxonomy to get the respective posts
$terms = get_terms( $taxonomy );

foreach( $terms as $term ) : ?>

    <div class="col-lg-3 col-xs-12 box">

          <div class="inner-content">

    <?php $args = array(
            \'post_type\' => $post_type,
            \'posts_per_page\' => -1, 
            \'tax_query\' => array(
                array(
                    \'taxonomy\' => $taxonomy,
                    \'field\' => \'slug\',
                    \'terms\' => $term->slug,

                )
            )

        );

    $posts = new WP_Query($args);
    if( $posts->have_posts() ): while( $posts->have_posts() ) : $posts->the_post(); ?>

    <a href="<?php echo get_permalink(); ?>" title="Read more about <?php echo get_the_title(); ?>">
        <?php  echo get_the_title(); ?>
    </a>

    <?php endwhile; endif; ?>

               </div>

         </div>



    <?php endforeach;

endforeach; ?>

1 个回复
最合适的回答,由SO网友:ssnepenthe 整理而成

这个post_parent arg到WP_Query 可以设置为0以仅返回顶级条目。

https://codex.wordpress.org/Class_Reference/WP_Query#Post_.26_Page_Parameters

结束

相关推荐

Custom sub taxonomy order by

在单类别页面中,特定的父类别子类别按slug或order wise显示,我会尝试,但我的代码中有错误,请检查我的代码并告诉我如何可能。$term_children = get_term_children( $EM_Category->id, \'event-categories\' ); if ( count($term_children) > 0 ) { foreach ( $term_children as $child ) { $term