WordPress WP_Query 通话帖子和页面
我设置了一个功能滑块,可以在标记为“功能”的帖子中绘制。
$my_query = new WP_Query(array(
'showposts' => 3,
'tag' => 'feature' ));
可以在帖子和页面中绘制吗? 我知道您可以使用 'post_type'=>'page'
绘制页面,但是您可以混合使用两者吗?
I have a feature slider set up that draws in posts that are tagged 'feature'
$my_query = new WP_Query(array(
'showposts' => 3,
'tag' => 'feature' ));
Is is possible to draw in posts AND pages?
I know you can draw pages with 'post_type'=>'page'
but can you mix the two?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以为
post_type
参数指定一个数组值,如下所示:有关详细信息,请参阅此页面:WP 法典
You can specify an array value for the
post_type
parameter, as such:See this page for more info: WP Codex
@ Fivedigit 谢谢,但我最终还是选择了这个:
虽然你的版本将来可能会派上用场!
@fivedigit Thanks but I went with this in the end:
Although your version may come in handy in the future!
对于必须编辑不使用传递到 WP_Query 的数组的旧代码的任何人,您可以添加
&post_type=any
来获取帖子和页面(以及其他内容)。不幸的是,我没有找到一种在不使用数组的情况下获取帖子和页面(没有其他类型)的方法,因为 post_type 将需要一个数组,如上面的示例所示。但是,如果您无论如何都在搜索特定类别,这应该足够了。示例(来自 vSlider v4.1.2,其中添加了
&post_type=any
以便页面包含在滑块中):感谢 @ Fivedigit 和 @my-jonny-wood 提供上述答案我要解决这个问题并修复我网站上的滑块!
For anyone having to edit older code that doesn't use an array passed into WP_Query, you can add
&post_type=any
to get posts and pages (and other content). Unfortunately I don't see a way to get posts and pages (without other types) without using an array, since post_type would then require an array as the examples above show. However, this should be good enough if you are searching for a particular category anyway.Example (this from vSlider v4.1.2 where
&post_type=any
is added so that pages are included in the slider):Thanks to @fivedigit and @my-jonny-wood for the answers above that led me to figuring this out and fixing the slider on my site!