网站优化

网站优化

Products

当前位置:首页 > 网站优化 >

在WordPress子菜单项后添加div

GG网络技术分享 2025-03-18 16:12 3


问题描述:

I am trying to add a div after any submenu items in a WordPress menu. I am working on the following code in the functions.php file:

add_filter( \'nav_menu_link_attributes\', \'SubMenuCheck\', 10, 3 );

function SubMenuCheck( $atts, $item, $args ) {

if (in_array(\'menu-item-has-children\', $item->classes)) {

$args->after = \'<div class=\"click\">+</div>\';

}

return $atts;

}

The code above adds the new div <div class=\"click\">+</div>but keeps applying it to all parent and child items after the first \'menu-item-has-children\' class:

menu issue example

Does anyone know why this is repeating?... I only want the div to display for items with the menu-item-has-children class.

Thanks

图片转代码服务由CSDN问答提供

感谢您的意见,我们尽快改进~

功能建议

我正在尝试在WordPress菜单中的任何子菜单项之后添加div。 我正在处理functions.php文件中的以下代码:</ p>

  add_filter(\'nav_menu_link_attributes\',\'SubMenuCheck\',10,3); 

function SubMenuCheck($ atts, $ item,$ args){

if(in_array(\'menu-item-has-children\',$ item-&gt; classes)){

$ args-&gt; after =\'&lt; div class =“click” &gt; +&lt; / div&gt;\';

}

返回$ atts;

}

</ code> </ pre>

上面的代码添加了新的div &lt ; div class =“click”&gt; +&lt; / div&gt; </ code>但在第一个\'menu-item-has-children\'类后继续将它应用于所有父项和子项:</ p>

\\ n

</ p>

有谁知道为什么会重复这个?...我只想显示带有<代码的项目的div > menu-item-has-children </ code> class。</ p>

谢谢</ p>

</ div>

网友观点:

I don\'t use the nav_menu_link_attributes filter, to add a button (which is semantically correct) to be the hot spot for toggling the children, I use the following:

function prefix_add_button_after_menu_item_children( $item_output, $item, $depth, $args ) {

if ( $args->theme_location == \'primary\' ) {

if ( in_array( \'menu-item-has-children\', $item->classes ) || in_array( \'page_item_has_children\', $item->classes ) ) {

$item_output = str_replace( $args->link_after . \'</a>\', $args->link_after . \'</a><button class=\\\"sub-menu-toggle\\\" aria-expanded=\\\"false\\\" aria-pressed=\\\"false\\\"><span class=\\\"screen-reader-text\\\">\' . _x( \'open dropdown menu\', \'verb: open the menu\', \'text-domain\' ) . \'</span></button>\', $item_output );

}

}

return $item_output;

}

add_filter( \'walker_nav_menu_start_el\', \'prefix_add_button_after_menu_item_children\', 10, 4 );

标签:

提交需求或反馈

Demand feedback