在Wordpress中检索一组帖子


Retrieve a set of posts in Wordpress

我正在编写一个自定义小部件,通过键入逗号分隔的帖子ID列表来显示Wordpress中的一组自定义帖子。我必须检索帖子的 php 如下:

<pre>
if ( have_posts() ) : while ( have_posts() ) : the_post();
  $postid = get_the_ID();
  if ( strpos($instance['posts_ids'], (string)$postid) !== false ):
    show the post
</pre>

不幸的是,这并不总是有效。如果我的 id 列表包含 ID #12497 作为其中之一,这将检索该帖子,但也可能会检索 ID 为 #249 的帖子,因为字符串匹配。

有什么建议吗?

谢谢贾

你不能像这样成功地找到一个字符串位置。只需在严格模式下爆炸 + in_array。

if ( have_posts() ) : while ( have_posts() ) : the_post();
  $postid = get_the_ID();
  $ids = explode($instance['posts_ids'], ',');
  if ( in_array($ids, $postid, true) !== false ){
  [...]
  }
}