在 foreach 计数中排除所有 0

我想通过为我的每个帖子提供特定编号来更新我的所有 custom_field。问题:我不想在里面添加 0 的数字。


$i = 1; // Start from 1


if ($ajax_query->have_posts()) : 

    while ($ajax_query->have_posts()) : 

        $ajax_query->the_post(); 


        update_post_meta( get_the_ID(), 'the_custom_field', $i++ ); 

        // $i++ will be the specific number for each my post.


    endwhile;

endif;

现在,我的帖子 1 有 the_custom_field 1,我的帖子 2 有 the_custom_field 2.... 我的帖子 10 有 the_custom_field 10

但它应该是这样的:

  • post1 的 the_custom_field 应该是 1

  • post2 的 the_custom_field 应该是 2

  • post3 的 the_custom_field 应该是 3

  • ...

  • post10 的 the_custom_field 应该是 11

  • post11 的 the_custom_field 应该是 12 等


慕少森
浏览 181回答 1
1回答

慕勒3428872

<?phpif ($ajax_query->have_posts()) :&nbsp;&nbsp; &nbsp; while ($ajax_query->have_posts()) :&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $ajax_query->the_post();&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; update_post_meta( get_the_ID(), 'the_custom_field', $i++);&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; while (strpos(strval($i),'0') !== false):&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $i++;&nbsp; &nbsp; &nbsp; &nbsp; endwhile;&nbsp; &nbsp; endwhile;endif;好吧,一旦我们分配了 ID,我们就会在之后运行一个 while 循环,直到我们得到一个不包含0.
打开App,查看更多内容
随时随地看视频慕课网APP