亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在 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 關注
  • 201 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號