繁体   English   中英

WordPress的query_posts()与数组?

[英]Wordpress query_posts() with an array?

我正在尝试根据数组中包含的多个ID查询帖子。

我的数组(称为$ my_array)如下所示:

Array
(
    [0] => 108
    [1] => 129
    [2] => 145
)

我的查询看起来像这样:

<?php query_posts(array('post__in' => $my_array)); ?>

但是,这仅返回一个帖子,该帖子具有数组中第一项的ID(108)。

我的语法是否错误?

$args = array(
  'post_type' => 'page',//or whatever type
  'post__in' => array(108,129,145)
  );
query_posts($args);

要么

$arr=array(108,129,145);
$args = array(
  'post_type' => 'page',
  'post__in' => $arr
  );
query_posts($args);

你总是要设置post_typepost__in说法。 因此,您的行应如下所示:

<?php query_posts(array('post_type' => 'post', 'post__in' => $my_array)); ?>

这将查询您在数组中具有的ID的帖子。

丹尼尔(Daniel),尽管您可能已找到答案,但我正在发布答案。 我还没有发表评论的声誉,query_posts支持WP_Query的所有参数,包括可以将'orderby'=>'title','order'=>'ASC'添加到query_posts调用中的命令

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM