我想通過為我的每個帖子提供特定編號來更新我的所有 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.
- 1 回答
- 0 關注
- 201 瀏覽
添加回答
舉報
0/150
提交
取消