WordPress 查询通过自定义字段/键获取帖子和页面
我正在开发一个滑块,我将在其中显示从帖子和页面中提取的一些内容(特色图像和摘录)。我想通过一个名为“滑块”的自定义字段来过滤帖子/页面!
因此,如果某些页面和帖子带有“滑块”自定义字段,则只有这些页面和帖子才会显示在滑块中。在wordpress中可以吗?如果是的话又如何呢?
一些指导将不胜感激!
I am working on a slider where I will show some contents(featured image and excerpt) pulled from both posts and pages. I want to filter the posts/pages by a custom field called 'slider'!
So if there are some pages and posts with 'slider' custom field only those will appear in the slider. Is it possible in wordpress? If it is then how?
Some guidance will be appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您正在寻找的是名为 WP_Query 的类,在 Codex 中有详细的解释。看一下
post_type
参数,它接受一个数组,因此您可以给它一个array( 'post', 'page' )
或您想要的任何其他帖子类型去取。现在元获取可以通过两种方式完成,要么通过新的
meta_query
参数(我相信从 3.1 开始),或者通过meta_key
和meta_value
自 3.1 起已弃用。这是一个粗略的例子(尚未检查这是否有效):
希望这是有道理的。干杯!
〜K
What you're looking for is the class called WP_Query, has got a detailed explanation in the Codex. Take a look at the
post_type
argument which accepts an array, thus you can give it aarray( 'post', 'page' )
or any other post types that you want to fetch.Now the meta fetch could be done in two ways, either via the new
meta_query
argument (from 3.1 onwards I believe) ormeta_key
andmeta_value
which are deprecated since 3.1.Here's a rough example (haven't checked if this works):
Hope that makes sense. Cheers!
~ K