【问题标题】:WordPress Covert post ID in loop to arrayWordPress 隐藏帖子 ID 循环到数组
【发布时间】: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,然后再发布文章。

【问题讨论】:

    标签: php arrays wordpress


    【解决方案1】:

    也许对这个解决方案有帮助!

    $tokenID = 244; 
    $all_posts = get_posts('post_type=sdm_downloads');
    $all_id = array();
    foreach( $all_posts as $all_post ) {
    $all_id[] = $all_post->ID;
    }
    //$arr2 = str_split($all_titles, 3);
    if( in_array($tokenID, $all_id) ) {
        $set = 'true';
      } else {
        $set = 'false';
      }
    echo $set;
    

    【讨论】:

      猜你喜欢
      • 2011-10-22
      • 1970-01-01
      • 1970-01-01
      • 2020-11-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-03-05
      相关资源
      最近更新 更多