特定项目前的Wordpress导航
我正在使用ACF和特定项目前的Wordpress导航,wordpress,menu,wordpress-theming,Wordpress,Menu,Wordpress Theming,我正在使用ACF和wp\u nav\u菜单\u objects过滤器向项目添加特定类。例如,按钮获取一个.btn类,在获取图标类之前需要图标的项目,等等。我已经找到了一种简单的方法,可以在标记中添加文本和项目,但我不知道如何在标记中的实际标记之前添加。我知道有一个简单的函数 wp_nav_menu( array ( 'before' => '<span>I am adding before the <li></span>' ) ); 为什么您需要为Wo
wp\u nav\u菜单\u objects
过滤器向项目添加特定类。例如,按钮
获取一个.btn
类,在获取图标
类之前需要图标的项目,等等。我已经找到了一种简单的方法,可以在
标记中添加文本和项目,但我不知道如何在
标记中的实际
标记之前添加。我知道有一个简单的函数
wp_nav_menu( array ( 'before' => '<span>I am adding before the <li></span>' ) );
为什么您需要为WordPress导航编码,即使您可以很容易地在管理部分添加类和图标。@Jamie,将CSS
before
伪选择器与content
结合使用会没有帮助吗?不清楚在跨度之前需要插入什么
function add_menu_item_type( $items, $args ) {
$parents = wp_list_pluck( $items, 'menu_item_parent' );
$menu = wp_get_nav_menu_object( $args->menu );
foreach ( $items as &$item ) {
$type = get_field( 'menu_item_type', $item );
$icon = get_field( 'menu_item_icon', $item );
$show_on_mobile = get_field( 'show_on_mobile', $item );
$mobile_nav = get_field( 'include_in_mobile_nav', $menu );
if ( 'logo' === $type ) {
array_push( $item->classes, 'logo' );
}
if ( 'button' === $type ) {
array_push( $item->classes, 'btn d-none d-lg-flex' );
} elseif ( ! $show_on_mobile && ( 'primary_left_nav' === $args->theme_location || 'primary_right_nav' === $args->theme_location ) ) {
array_push( $item->classes, 'd-none d-lg-block' );
}
if ( $mobile_nav || 'primary_mobile_nav' === $args->theme_location ) {
array_push( $item->classes, 'col-24' );
}
// If Dropdown add class.
if ( 'dropdown' === $type ) {
$item->title .= ' <span class="icon icon-arrow-down-thin"></span>';
}
// If Dropdown add class.
if ( 'icon' === $type && ! empty( $icon ) ) {
// HERE IS WHERE I WOULD LIKE TO DO SOMETHING LIKE $item->before
$item->title = '<span class="icon icon-' . $icon . '"></span> ' . $item->title;
}
// If Dropdown and has children.
if ( in_array( $item->ID, $parents, true ) && 'has-children' === $item->classes[0] && 'dropdown' === $type ) {
array_push( $item->classes, 'menu-item-dropdown' );
}
}
return $items;
}
add_filter( 'wp_nav_menu_objects', 'add_menu_item_type', 10, 2 );