[英]Wordpress wp_nav_menu walker: dynamic before content
我在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參數應用范圍 。 但是我需要將我的動態類名應用於范圍 。 我想我可以使用$ 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() ) ); ?>
盡管按@Trix的建議,自定義Nav Walker類可能是一個更好的解決方案(更好?),但我想回答以下問題:如何使用過濾器應用跨度。 這段代碼做到了:
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 );
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.