通过模板名称查询 WordPress 中的页面
我在“foo.php”中有一个名为“Foo”的模板,我希望能够选择使用该模板的所有页面。我已经搜索了一段时间,但未能找到成功的方法来做到这一点...有人可以启发我正确/唯一的方法来做到这一点吗?
I have a template named "Foo" in "foo.php", I would like to be able to select all pages that are using that template. I have searched for awhile now but have not been able to find a successful way to do this... Can somebody enlighten me on the proper/only way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用以下代码来获取它
You can get this by using following code
机器人的回答很好,但我想我应该澄清一些事情。
首先,您应该使用您创建的查询的变量,因此它将是 $query->have_posts() 等。
其次,您应该指定 post_type。我使用了any,这样它就会拉出除修订之外的任何帖子类型。
最后,如果这是在具有任何其他 WP 循环的页面中,您可能需要使用 wp_reset_query。我在下面添加了一个,在上面添加了一个,以防万一,但只有当上面或下面有另一个循环时,你才真正需要这个。如果不这样做,请将其删除。
这是代码:
希望对某人有帮助!快乐编码!
Robot's answer is good, but I thought I'd clarify a few things.
First, You should use the variable for the query you created, so it would be $query->have_posts() etc.
Second, you should specify post_type. I used any, so that it will pull any post types except for revisions.
Last, if this is in a page with any other WP loops, you may want to use wp_reset_query. I added one below and one above just in case, but you only really need this if you have another loop above or below. Remove it if you don't.
Here is the code:
Hope that helps someone!! Happy coding!
这也适用
http://jorgepedret.com /old/web-development/get-pages-by-template-name-in-wordpress/
This also works
http://jorgepedret.com/old/web-development/get-pages-by-template-name-in-wordpress/