为了账号安全,请及时绑定邮箱和手机立即绑定

在 foreach 计数中排除所有 0

在 foreach 计数中排除所有 0

PHP
慕少森 2021-07-08 14:08:18
我想通过为我的每个帖子提供特定编号来更新我的所有 custom_field。问题:我不想在里面添加 0 的数字。$i = 1; // Start from 1if ($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 应该是 1post2 的 the_custom_field 应该是 2post3 的 the_custom_field 应该是 3...post10 的 the_custom_field 应该是 11post11 的 the_custom_field 应该是 12 等
查看完整描述

1 回答

?
慕勒3428872

TA贡献1848条经验 获得超6个赞

<?php



if ($ajax_query->have_posts()) : 

    while ($ajax_query->have_posts()) : 

        $ajax_query->the_post(); 


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

        while (strpos(strval($i),'0') !== false):

            $i++;

        endwhile;


    endwhile;

endif;

好吧,一旦我们分配了 ID,我们就会在之后运行一个 while 循环,直到我们得到一个不包含0.


查看完整回答
反对 回复 2021-07-16
  • 1 回答
  • 0 关注
  • 180 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信