如果当前类别有子类别,则Wordpress

如果当前类别有子类别,则Wordpress,wordpress,Wordpress,好的,我有这个循环代码: <?php //get all categories then display all posts in each term $taxonomy = 'category'; $param_type = 'category__in'; $term_args=array( 'orderby' => 'name', 'order' => 'ASC', 'hide_empty' => 0

好的,我有这个循环代码:

<?php
    //get all categories then display all posts in each term
    $taxonomy = 'category';
    $param_type = 'category__in';
    $term_args=array(
      'orderby' => 'name',
      'order' => 'ASC',
      'hide_empty' => 0,
      'hierarchical' => 0
    );
    $terms = get_terms($taxonomy,$term_args);
    if ($terms) {
      foreach( $terms as $term ) {
        $args=array(
          "$param_type" => array($term->term_id),
          'post_type' => 'products',
          'post_status' => 'publish'
          );
        $my_query = null;
        $my_query = new WP_Query($args);
        if( /*$my_query->have_posts()*/ 1==1 ) {  ?>
          <div id="<?php echo str_replace(" ","",$term->name); ?>" class="category section">
            <h3 class="categoryTitle"><?php echo $term->name;?></h3>
            <?php
          while ($my_query->have_posts()) : $my_query->the_post(); ?>
            <div class="product">
                <h3 class="productTitle"><?php the_title(); ?></h3>
                <div class="description"><?php the_content(); ?></div>
            </div>
            <div class="clearfix"></div>
           <?php
          endwhile;
          ?>
          </div>
     <?php
        }
      }
    }
    wp_reset_query();  // Restore global post data stomped by the_post().
    ?>      


我将尝试用以下代码替换您的代码:

if ($terms) :

foreach( $terms as $term ) :

$args=array(
     "$param_type" => array($term->term_id),
     'post_type' => 'products',
     'post_status' => 'publish'
);

$children = get_posts($args);

if($children) : ?>

<div id="<?php echo str_replace(" ","",$term->name); ?>" class="category section">
     <h3 class="categoryTitle"><?php echo $term->name;?></h3>

     <?php foreach($children as $child) : setup_postdata($child); ?>

          <div class="product">
                <h3 class="productTitle"><?php the_title(); ?></h3>
                <div class="description"><?php the_content(); ?></div>
          </div>

     <?php endforeach; ?>

</div>


<?php endif; // if($children)

endforeach;

endif; // if($terms)
if($terms):
foreach($terms作为$term):
$args=数组(
“$param_type”=>数组($term->term_id),
“post_类型”=>“产品”,
“发布状态”=>“发布”
);
$children=get_posts($args);
如果($儿童):?>

我也有同样的问题,但我想我已经找到了一个简单的解决方案,检查当前的帖子类别是否有父类

$term = get_queried_object();


if($term->post_parent != 0){
    echo 'has parent'; //this post category has child
}else{
    echo 'no parent'; //this post category doesn't have child
}

我希望这会有帮助。

为Dave的广泛努力干杯,我将尝试一下!