【发布时间】:2016-09-12 03:44:31
【问题描述】:
问题:
想使用我所有已发表文章中的所有$post->ID 创建一个数组。但是在我的 WordPress 中,我有 $tokenID,如果匹配到 $post->ID,这是显示某些文章的关键。
代码图1
$tokenID = 244;
$io_ID = new WP_Query( ['post_type' => 'sdm_downloads', 'posts_per_page' => -1] );
while ( $io_ID->have_posts() ) : $io_ID->the_post();
print_r($post->ID);
endwhile;
wp_reset_postdata();
结果: 255254253251244
当我在 while() 循环内回显时,这就是 $post->ID,它应该是 255、254、253、251、244
我尝试了str_split,结果几乎接近我想要实现的目标。这是输出。
代码图2
$tokenID = 244;
$io_ID = new WP_Query( ['post_type' => 'sdm_downloads', 'posts_per_page' => -1] );
while ( $io_ID->have_posts() ) : $io_ID->the_post();
$arr2 = str_split($post->ID, 3);
// print_r($arr2);
if( in_array($tokenID, $arr2) ) {
$set = 'true';
} else {
$set = 'false';
}
endwhile;
wp_reset_postdata();
echo $set;
print_r 的结果: 数组 ( [0] => 255 ) 数组 ( [0] => 254 ) 数组 ( [0] => 253 ) 数组 ( [0] => 251 )数组 ( [0] => 244 )
条件结果:“真”
*即使$tokenID 不同,$set 输出仍然会导致true。
问题
当我的$post->ID 被转换为数组时,如何实现它的条件排列?如果值存在,我想先查找并匹配$tokenID 和$post->ID,然后再发布文章。
【问题讨论】: