我在 WordPress 博客中构建了一个导航。项目显示如下:
该图标是通过使用引导类
glyhicon-glyphiconname
来应用的。我动态添加类。 php 代码:
function add_specific_menu_location_atts( $atts, $item, $args ) {
// check if the item is in the primary menu
if( $args->theme_location == 'directentries' ) {
foreach($item as $key => $value) {
if($key == 'title') {
$catIcon = setCatIcon($value);
}
}
// add the desired attributes:
$atts['class'] = 'btn btn-primary btn-lg glyphicon glyphicon-'.$catIcon;
}
return $atts;
}
add_filter( 'nav_menu_link_attributes', 'add_specific_menu_location_atts', 10, 3 );
$args = array(
'theme_location' => 'directentries',
'depth' => 1,
'container' => false,
'menu_class' => 'nav',
'link_before' => '<br>',
//'link_before' => 'span class"glyphicon"',
'walker' => ''
);
wp_nav_menu($args);
这里的问题是,链接文本的字体(当然)是glyhicon,否则图标将不适用于链接。所以:正确的方法是在初始化菜单时使用 link-before 参数应用 span。 但我需要将动态类名应用于 span 。 我想我可以使用 $args 参数访问过滤器类中的 link_before 参数..
当前的标记是这样的: 我需要它链接如下: 有谁知道如何应用跨度并改变课程?
。
1。首先将以下代码添加到您的functions.php中。
class Nav_Walker_Nav_Menu extends Walker_Nav_Menu {
function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
global $wp_query;
$indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';
$class_names = '';
$classes = empty( $item->classes ) ? array() : (array) $item->classes;
$classes[] = 'menu-item-' . $item->ID;
$class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item, $args ) );
$class_names = $class_names ? ' class="' . esc_attr( $class_names ) . '"' : '';
$id = apply_filters( 'nav_menu_item_id', 'menu-item-'. $item->ID, $item, $args );
$id = $id ? ' id="' . esc_attr( $id ) . '"' : '';
$output .= $indent . '<li' . $id . $class_names .'>';
$attributes = ! empty( $item->attr_title ) ? ' title="' . esc_attr( $item->attr_title ) .'"' : '';
$attributes .= ! empty( $item->target ) ? ' target="' . esc_attr( $item->target ) .'"' : '';
$attributes .= ! empty( $item->xfn ) ? ' rel="' . esc_attr( $item->xfn ) .'"' : '';
$attributes .= ! empty( $item->url ) ? ' href="' . esc_attr( $item->url ) .'"' : '';
$item_output = $args->before;
$item_output .= '<a'. $attributes .'>';
$item_output .= $args->link_before . apply_filters( 'the_title', $item->title, $item->ID ) . $args->link_after;
if ( 'primary' == $args->theme_location ) {
$submenus = 0 == $depth || 1 == $depth ? get_posts( array( 'post_type' => 'nav_menu_item', 'numberposts' => 1, 'meta_query' => array( array( 'key' => '_menu_item_menu_item_parent', 'value' => $item->ID, 'fields' => 'ids' ) ) ) ) : false;
$item_output .= ! empty( $submenus ) ? '<span class="glyphicon"></span>' : '';
}
$item_output .= '</a>';
$item_output .= $args->after;
$output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );
}
}
2。将以下代码添加到安装 wp_nav_menu 的 header.php 中。
下面解释的是代码,因此在这种情况下它会安装新的自定义菜单
Nav_Walker_Nav_Menu
。
<?php wp_nav_menu( array( 'container_class' => 'menu-header', 'theme_location' => 'primary', 'walker' => new Nav_Walker_Nav_Menu() ) ); ?>
尽管自定义 Nav Walker 类可能是 @Trix 建议的一个好的(更好?)解决方案,但我想回答如何使用我的过滤器应用跨度的问题。这段代码做到了:
function add_specific_menu_location_atts( $atts, $item, $args ) {
if( $args->theme_location == 'directentries' ) {
foreach($item as $key => $value) {
if($key == 'title') {
$catIcon = setCatIcon($value);
}
}
foreach($args as $key => $value) {
if($key == 'link_before') {
$args->$key = '<span class="glyphicon glyphicon-'.$catIcon.'"></span><br>';
}
}
// add the desired attributes:
$atts['class'] = 'btn btn-primary btn-lg';
}
return $atts;
}
add_filter( 'nav_menu_link_attributes', 'add_specific_menu_location_atts', 10, 3 );
好的,这就是我所做的并修改了答案以获取“CSS 类(可选)”字段的值。它更有用的代码,因为当前的答案是获取“导航标签”字段的值,有时我们需要图标的动态自定义类。
我是怎么做到的- 将数组转换为字符串,我们知道字符串将获取所有 CSS 类,因此使用
strtok
函数从字符串中获取第一个类。当我们将自定义类添加到“CSS 类(可选)”字段时,它始终出现在数组中的第一个单词。
function add_specific_menu_location_atts( $atts, $item, $args ) {
if( $args->theme_location == 'topcontact' ) {
foreach($item as $key => $value) {
if($key == 'classes') {
$catIcon = implode(" ", $value);
}
}
foreach($args as $key => $value) {
if($key == 'link_before') {
$args->$key = '<i class="fas fa-'.strtok($catIcon, " ").'"></i>';
}
}
// add the desired attributes:
$atts['class'] = '';
}
return $atts;
}
add_filter( 'nav_menu_link_attributes', 'add_specific_menu_location_atts', 10, 3 );