如何从搜索结果中排除wordpress页面模板(自定义模板)?
我创建了自定义页面模板。
<?php
/*
* Template Name: foo
*/
?>
该文件名为“foo.php”。
我尝试过
global $query_string;
query_posts($query_string . "&post_type=post");
但所有页面都会被排除....
如何从 WordPress 搜索结果中仅排除此页面模板?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
对于任何偶然发现此线程并且在 WP 新版本上没有成功的人:必须设置 $query args,而不是重做 query_posts... 如下所示:
For anyone whom stumbles on this thread and doesn't succeed on WP newer versions: the $query args must be set instead redoing query_posts... as the follows:
Nicolay提到的查询Nicolay非常方便,但它也会从搜索结果中删除所有帖子,因为帖子不包含
'_wp_page_template'
键。要拥有所有页面(没有过滤的模板)以及所有帖子,您需要执行以下操作:可以找到有关此内容的大量信息 。
The query mentioned by Nicolay is very handy, but it also removes all posts from the search results, because posts do not contain the
'_wp_page_template'
key. To have all pages (sans the filtered template) as well as all posts you need to do the following:Extensive info on this can be found in the WordPress Codex.
试试这个:
Try this:
谢谢尼古拉!由于某种原因,昨晚我无法让它发挥作用,但今天,又过了一两个小时,我做到了。可能只是因为我使用了错误的过滤器或者缺少代码的最后一行。
就我而言,我想排除基于多个模板的内容,因此添加了更多键/值/比较数组元素。我也只想在搜索期间执行此操作,因此为此添加了一个条件子句。这是我添加到主题的functions.php 文件中的完整函数:
Thanks Nikolay! For some reason last night I just was not getting this to work but today, after another hour or two, I did. It may have simply been the fact that I was using the wrong filter or was missing the last line of your code.
In my case I wanted to exclude content based upon multiple templates, so, added more key/value/compare array elements. I also only wanted to do this during a search, so, added a conditional clause for that. Here's the complete function I added to my theme's functions.php file:
我必须排除多个页面模板,所以我必须稍微调整上面的代码,但最终,这对我有用:
也许它对那里的人有用。
I had to exclude more than one page template, so I had to adapt the above code a little bit, but in the end, this worked for me:
Maybe it is useful to somebody out there.