如何向 Wordpress 菜单锚添加独特的类? [英] How to add unique class to Wordpress menu anchor?

查看:24
本文介绍了如何向 Wordpress 菜单锚添加独特的类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个function:

function my_walker_nav_menu_start_el($item_output, $item, $depth, $args) {
    // you can put your if statements in here (use item, depth and args in conditions)
    $item_output = preg_replace('/<a /', '<a class="your-class" ', $item_output, 1);
    return $item_output;
}
add_filter('walker_nav_menu_start_el', 'my_walker_nav_menu_start_el', 10, 4);

工作正常,因为它在菜单中为我的所有 anchors 设置了一个类.问题是:它在所有锚点上设置相同的类.我想要每个锚点不同的类.那可能吗?如果是这样,我该怎么做?

Which is working fine, because it is setting a class on all my anchors in the menu. The problem is: it's setting the same class on all anchors. I want different classes per anchor. Is that possible? If so, how can I do this?

编辑 --> 我想要这样:

Edit --> I want to have it like this:

<li><a class="icon-bird"></a></li>
<li><a class="icon-book"></a></li>
<li><a class="icon-red"></a></li>

推荐答案

假设 $item 包含您的值(鸟、书、红色等):

Assuming $item contains your values (bird, book, red, etc):

function my_walker_nav_menu_start_el($item_output, $item, $depth, $args) {
    $item_output = preg_replace('/<a /', '<a class="item-' . $item . '"', $item_output, 1);
    return $item_output;
}
add_filter('walker_nav_menu_start_el', 'my_walker_nav_menu_start_el', 10, 4);

这篇关于如何向 Wordpress 菜单锚添加独特的类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆