我有一个循环,它获取顶级的cat\\u id,例如“Data”,循环类别,将第二级显示为H2s,然后显示每个子类别中的帖子。
数据分析服务现在,我得到了第三个级别,需要将第三个级别输出为H3s,然后是Post,如
数据分析服务业务信号我们如何梳理出新的层次。这是当前的两级回路:
<?php
$paged = ( get_query_var( \'paged\' ) ) ? get_query_var(\'paged\') : 1;
$cat_id = 42;
$child_categories=get_categories(
array( \'parent\' => $cat_id )
);
foreach ( $child_categories as $child ) { ?>
<!-- loop through child categories and make columns -->
<div class="col-sm-4">
<!-- category title -->
<?php
$string = $child ->cat_name;
$className = str_replace(\' \', \'\', $string);
?>
<h2 class="cat-icon <?php echo $className; ?>"><?php echo $child ->cat_name; ?></h2>
<!-- query -->
<?php $args = array( \'category_name\' => $child->cat_name, \'posts_per_page\' => -1, \'paged\' => $paged, \'orderby\' => \'title\', \'order\' => \'ASC\' );
$loop = new WP_Query( $args ); ?>
<!-- loop to create buttons -->
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php get_template_part("home", "service-button"); ?>
<?php endwhile; ?>
</div>
<?php } ?><!-- end foreach loop -->
最合适的回答,由SO网友:Sally CJ 整理而成
Try this:
<?php
$paged = ( get_query_var( \'paged\' ) ) ? get_query_var(\'paged\') : 1;
$cat_id = 42;
$child_categories=get_categories(
array( \'parent\' => $cat_id )
);
foreach ( $child_categories as $child ) { ?>
<!-- loop through child categories and make columns -->
<div class="col-sm-4">
<!-- category title -->
<?php
$string = $child->cat_name;
$className = str_replace(\' \', \'\', $string);
?>
<h2 class="cat-icon <?php echo $className; ?>"><?php echo $child->cat_name; ?></h2>
<?php
$subchild_cats = get_categories( array( \'parent\' => $child->term_id ) );
foreach ( $subchild_cats as $subchild ) :
$className = str_replace( \' \', \'\', $subchild->cat_name );
?>
<h3 class="cat-icon <?php echo $className; ?>"><?php echo $subchild->cat_name; ?></h3>
<!-- query -->
<?php $args = array( \'category_name\' => $subchild->cat_name, \'posts_per_page\' => -1, \'paged\' => $paged, \'orderby\' => \'title\', \'order\' => \'ASC\' );
$loop = new WP_Query( $args ); ?>
<!-- loop to create buttons -->
<?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
<?php get_template_part("home", "service-button"); ?>
<?php endwhile; ?>
<?php endforeach; ?>
</div>
<?php } ?><!-- end foreach loop -->