从 wp_get_archives 中排除类别?
有什么方法可以从 wp_get_archives 中排除某个类别吗?我试图在侧边栏中显示月份,但我想排除不是博客条目的帖子。
$catID = get_cat_id('Projects');
$variable = wp_get_archives('type=monthly&show_post_count=1);
echo $variable;
Is there any way to exclude a category from wp_get_archives? I'm trying to show the months in the sidebar, but I want to exclude the posts that are not blog entries.
$catID = get_cat_id('Projects');
$variable = wp_get_archives('type=monthly&show_post_count=1);
echo $variable;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(9)
您可以在 pre_get_posts 上使用过滤器挂钩吗?
我知道这样的东西适用于 is_author、is_home 和 is_feed...
取决于您是否可以对 is_archive 或 is_monthly 之类的东西执行此操作
您可以将其放入带有插件标头的 php 文件中:
然后将其上传到您的插件目录并激活它。
Can you use a filter hook on pre_get_posts instead?
I know something like this works for is_author, is_home, and is_feed...
depends on whether you can do it for something like is_archive or is_monthly
You would drop that in a php file with a plugin header:
Then upload it to your Plugins directory and activate it.
没有官方的方法可以做到这一点。但我尝试并测试了很多代码块,只有这个对我有用。
将 your_category_id 替换为您的帖子类别的原始 ID,代码将起作用。
There isn't an official way of doing this. But i tried and tested a lot of code block and only this one worked for me.
Replace your_category_id with an original id of your post category and the code will work.
您可以在functions.php 文件中编写一个过滤器,该过滤器将更改wp_get_archive 函数的默认行为。
You can write a filter in your functions.php file which will change wp_get_archive function's default behavior.
在一个项目中遇到这个问题,但从未在网上找到解决方案——我的不是最漂亮的 PHP,但它可以解决问题。
这是凯蒂建议的过滤器的一个发挥,我也在一些支持论坛上遇到过。这位于您的
functions.php
中:在第二个函数中,将
taxonomy-name
替换为实际自定义分类的名称。自定义分类中的所有术语 ID 均捕获在字符串中;其余部分的操作与原始函数相同 - 只有自定义分类中的类别列表包含在
wp_get_archives()
列表中。您还可以调整代码以排除它们(上面的第一个示例)。如果您只希望
wp_get_archives()
列表的一个实例执行此操作,只需跳过functions.php
中应用过滤器的前两行代码即可。然后,当您使用wp_get_archives()
标记时,在其之前应用过滤器,然后将其删除:Ran into this problem in a project but never found a solution online -- mine isn't the prettiest PHP, but it does the trick.
This is a play off the filter suggested by Katie, which I ran across in few support forums as well. This goes in your
functions.php
:In the second function, swap
taxonomy-name
for the name of your actual custom taxonomy.All the IDs of terms in your custom taxonomy are captured in a string; the rest operates the same as the original function -- only that list of categories from your custom taxonomy are included in the
wp_get_archives()
list. You can also tweak the code to exclude them as well (first example above).If you only want one instance of the
wp_get_archives()
list to do this, just skip the top two lines of code in yourfunctions.php
that apply the filters. Then, when you use thewp_get_archives()
tag, apply the filters before it, and remove them afterwards:wp_get_archives()
没有基于类别排除的机制-- 它纯粹用于基于时间的存档(每年、每月、每日、每周)或“每个帖子”存档:postbypost 或按帖子标题排序的逐个帖子。wp_get_archives()
does not have a mechanism to exclude based on category -- it's purely for time-based archives (yearly, monthly, daily, weekly) or "every post" archives: postbypost or post by post ordered by post title.处理类别档案有不同的方法: WordPress › 支持 » 将档案限制为类别/日期类别档案
我使用 Clean Archives Reloaded WordPress › Clean Archives Reloaded « WordPress插件并排除第 200 行周围的类别:
There are different ways to work with category archives: WordPress › Support » Limit archives to category / date archives for category
I use Clean Archives Reloaded WordPress › Clean Archives Reloaded « WordPress Plugins and exclude categories around line 200:
您可能需要查看 get_categories 并倾向于您自己的自定义解决方案。虽然这可能会花费您更多的时间和工作;你确实会得到完全控制你想要实现的目标的结果。
You might want to look in to get_categories and lean towards your own custom solution. While this may cost you a little more time and work; you will indeed get the upshot of having full control over what you're trying to achieve.
将下面的代码放在后面
这段代码已经对我有用了:)
Place the code below just after
This code is working for me already :)
如果您只想在主题目录的functions.php中包含wp_get_archive函数的特定类别,请使用此选项
Use this if you want to include only specific categories for wp_get_archive function in your functions.php of your theme directory