dsfsdfsd34324 2015-01-15 19:12
浏览 42
已采纳

如果is_array没有按预期的wordpress子类别工作

This code works fine and returns the sub-categories in the array, and does not return a result if there are not any subcategories,

 $parentCatName = single_cat_title('',false);
    $parentCatID = get_cat_ID($parentCatName);
    $childCats = get_categories( 'child_of='.$parentCatID );
    if(is_array($childCats)):
      foreach($childCats as $child){ ?>
     <?php query_posts('cat='.$child->term_id . '&posts_per_page=1');
         while(have_posts()): the_post(); $do_not_duplicate = $post->ID; ?>
    <!-- POST CODE -->
    <?php get_template_part( 'content', 'thumbs' ); ?>
    <!-- END POST CODE -->
    <?php
    endwhile;
    wp_reset_query();
    }
    endif;
    ?> 

however if I try to insert a header after the if is array, it returns the header whether there is a sub-category or not ie:

$parentCatName = single_cat_title('',false);
$parentCatID = get_cat_ID($parentCatName);
$childCats = get_categories( 'child_of='.$parentCatID );
if(is_array($childCats)):
echo 'Sub-Categories:' ;
  foreach($childCats as $child){ ?>
 <?php query_posts('cat='.$child->term_id . '&posts_per_page=1');
     while(have_posts()): the_post(); $do_not_duplicate = $post->ID; ?>
<!-- POST CODE -->
<?php get_template_part( 'content', 'thumbs' ); ?>
<!-- END POST CODE -->
<?php
endwhile;
wp_reset_query();
}
endif;
?>

I solved it by using count, but it seems clumsy to me and that it should have worked with if is array.

  <?php
    $parentCatName = single_cat_title('',false);
    $parentCatID = get_cat_ID($parentCatName);
    $childCats = get_categories( 'child_of='.$parentCatID );
    $countChild = count($childCats);
    if ($countChild > 0) : echo '<h2>Sub-Categories:</h2>'; endif;
    if(is_array($childCats)):
      foreach($childCats as $child){ ?>
     <?php query_posts('cat='.$child->term_id . '&posts_per_page=1');
         while(have_posts()): the_post(); $do_not_duplicate = $post->ID; ?>
    <!-- POST CODE -->
    <?php get_template_part( 'content', 'thumbs' ); ?>
    <!-- END POST CODE -->
    <?php
    endwhile;
    wp_reset_query();
    }
    endif;
    ?>
  • 写回答

1条回答 默认 最新

  • drbfxb977777 2015-01-15 20:06
    关注

    As stated in the comments the problem is not that is_array() is not working, the problem is you are not testing to see if the array has any rows.

    Your way of doing it is just fine. There is no way of doing it that does not require to execute code. If I were doing it I would probably short circuit the IF statement like this:

    if (is_array($childCats) and count($childCats)>0) {
        ...
    }
    

    That way you skip echoing out the header and the bother of the foreach - which right now is hitting and not executing because the array is empty.

    HTH,

    =C=

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 速帮,学校需要在外上班没空
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改
  • ¥15 Windows 系统cmd后提示“加载用户设置时遇到错误”
  • ¥50 vue router 动态路由问题
  • ¥15 关于#.net#的问题:End Function
  • ¥15 无法import pycausal
  • ¥15 VS2022创建MVC framework提示:预安装的程序包具有对缺少的注册表值的引用
  • ¥15 weditor无法连接模拟器Local server not started, start with?
  • ¥20 6-3 String类定义