Wordpress —困惑,尝试使用 add_filter 自定义 wp_list_categories
我目前有一个网站,其中包含硬编码的类别和类别列表。子类别,其中每个项目的格式如下:
<li class="cat-item open-access"><a href="/categories/open-access/">Open Access</a></li>
重要的是,每个
项目都分配有一个类,该类与链接到其中的类别的 slug 相匹配。我显然想使用 Wordpress 的 wp_list_categories()
输出列表而不是对其进行硬编码,但需要保留每个
项目的自定义类。我一直在研究过滤器和操作,并认为我可能通过将其添加到主题的 functions.php
文件中来找到解决方案:
function add_class_from_slug($wp_list_categories) {
$pattern = '/class=\"/';
$replacement = 'class="'.$category->slug.' ';
$newclass = preg_replace($pattern, $replacement, $wp_list_categories);
return $newclass;
}
add_filter('wp_list_categories','add_class_from_slug');
但这不起作用 - 返回的文本当页面呈现时,$category->slug
会消失。如果我添加静态文本(使用像 $replacement = 'class="myclass ';
这样的行,它会渲染得很好。
令人沮丧的是,我可以通过添加 $class 来获得我想要的输出。 = ' '.$category->slug;
在正确的位置 wp-includes/classes.php
,但希望避免诉诸于此。
为什么我不能在我的函数中使用 $category->slug
?解决方法、建议、有关该主题的进一步阅读?应该补充一点,我对 PHP 有相当基本的掌握。谢谢!
I currently have a site with a hard-coded list of categories & subcategories, where each item is formatted as follows:
<li class="cat-item open-access"><a href="/categories/open-access/">Open Access</a></li>
Importantly, each <li>
item is assigned a class which matches the slug of the category linked to within.
I would obviously like to use Wordpress' wp_list_categories()
to output the list instead of hard-coding it, but need to keep the custom class for each <li>
item.
I have been looking into filters and actions and thought I might have come up with a fix by adding this to my theme's functions.php
file:
function add_class_from_slug($wp_list_categories) {
$pattern = '/class=\"/';
$replacement = 'class="'.$category->slug.' ';
$newclass = preg_replace($pattern, $replacement, $wp_list_categories);
return $newclass;
}
add_filter('wp_list_categories','add_class_from_slug');
But this doesn't work — the text returned by $category->slug
drops out when the page is rendered. If I add in static text (using a line like $replacement = 'class="myclass ';
, it renders fine.
Frustratingly, I can get the output I want by adding $class .= ' '.$category->slug;
at the right spot wp-includes/classes.php
, but want to avoid resorting to that.
Why can't I just use $category->slug
in my function? Workarounds, suggestions, further reading on the subject? Should add that I have a pretty basic grip on PHP. Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用自己的类扩展
Walker_Category
类并重写函数start_el()
。如果您使用的类的名称是
Walker_MyCategory
,您可以调用wp_list_categories()
,如下所示:您也可以自己构建自己的列表。请参阅 get_categories()
哦,你的代码不起作用,因为 $category 没有在任何地方定义。您需要确切地知道它是什么类别。
You can extend the
Walker_Category
class with your own class and override the functionstart_el()
.If the name of the class you used is
Walker_MyCategory
, you could callwp_list_categories()
like:You could also build your own list yourself. See get_categories()
Oh and your code didn't work because $category wasn't defined anywhere. You need to know exactly what category it is.