如何拦截所有 WordPress 查询并限制类别?
在 WordPress 插件编码中,如果我有两个类别(汽车和园艺),并且我想始终从所有查询中排除园艺,那么我需要做什么拦截(钩子或其他技巧)才能做到这一点?我需要始终排除园艺,无论是通过搜索、标签云小部件、评论小部件、日历小部件还是网站的任何其他部分。
我问的原因是我的客户需要有一个主题根据传入的域名做出不同的反应。如果用户输入gardening.com并且它映射到此博客,那么他希望限制为仅显示gardening.com内容。如果用户输入 cars.com 并且它映射到同一个博客,那么他希望限制为仅显示 cars.com 内容。
再说一遍,我的问题在于想要使用插件钩子到达 WordPress 的一个中心位置,以始终确保查询仅限于给定的类别。这样,即使添加了一个不是 WordPress 默认的新插件,它也会受到类别的限制。
我已经弄清楚如何将此代码放入主题的 header.php 中,以使链接根据某人输入的 URL 正确运行:
$sURL = 'http://' . $_SERVER['SERVER_NAME'];
update_option('siteurl',$sURL);
update_option('home',$sURL);
unset($sURL);
In WordPress plugin coding, if I have two categories (cars and gardening) and I want to always exclude gardening from all queries, what is the intercept (hook or other trick) I need to do to do this? I need to always exclude gardening whether it be via search, tag cloud widget, comment widget, calendar widget, and any other portion of the website.
The reason I ask is that my client needs to have a single theme react differently by domain name coming in. If the user types gardening.com and it's mapped to this blog, then he wants to restrict to show only gardening.com content. If the user types cars.com and it's mapped to this same blog, then he wants to restrict to show only cars.com content.
Said again, my question lies in the area of wanting to hit one central place of WordPress with a plugin hook to always ensure the queries are restricted to a given category. That way, even if a new plugin is added that's not a default coming with WordPress, it will also be restricted by category too.
I have already figured out how to slip this code in the header.php of a theme to make the links act properly depending on whichever URL someone types in:
$sURL = 'http://' . $_SERVER['SERVER_NAME'];
update_option('siteurl',$sURL);
update_option('home',$sURL);
unset($sURL);
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我发现我必须创建一个 home.php,它是 index.php 的副本,但在 have_posts() 调用之前添加一个 query_posts('category_name=' . $_SERVER['SERVER_NAME']) 调用。在index.php 上,我没有添加它,也没有添加query_posts()。然后,我必须制作一个 archive.php,它是 Kubrick 的 archive.php 的副本,然后对其进行编辑,以便根据选择的操作来不同地更改 query_posts(),例如添加“&tag=”, “&year=”等。此外,还必须添加一个 search.php(借用库布里克),它也按类别进行了限制。这是一个很大的帮助:
http://codex.wordpress.org/Template_Tags/query_posts
我唯一还没弄清楚的是如何将动态侧边栏中的小部件限制为一个类别。我正在 StackOverflow 中将其变成单独的请求。
I found I had to create a home.php that was a copy of index.php, but tack on a query_posts('category_name=' . $_SERVER['SERVER_NAME']) call before the have_posts() call. On index.php, I left it alone and didn't add the query_posts(). Then, I had to make an archive.php that was a copy of Kubrick's archive.php, and then edit that so that I change query_posts() differently depending on what was chosen to do, such as add "&tag=", "&year=", etc. As well, a search.php had to be added (borrowing from Kubrick) that did a restriction too by category. This was a big help:
http://codex.wordpress.org/Template_Tags/query_posts
The only thing I haven't figured out yet is how to restrict widgets in the dynamic sidebar to a category. I'm making that into a separate request in StackOverflow.