add_menu_classes

函数
add_menu_classes ( $menu )
参数
  • (array) $menu The array of administration menu items.
    Required:
返回值
  • (array) The array of administration menu items with the CSS classes added.
定义位置
相关方法
add_menu_pageadd_submenu_pageadd_cssclassdo_enclosecomment_class
引入
2.7.0
弃用
-

add_menu_classes。这个动作钩子允许你为WordPress管理菜单中的菜单项添加自定义CSS类。这对于自定义菜单的外观,或者为特定的菜单项添加功能是很有用的。

为顶层管理菜单项添加CSS类。

添加的类列表包括`.menu-top-first`和`.menu-top-last`。

function add_menu_classes( $menu ) {
	$first_item  = false;
	$last_order  = false;
	$items_count = count( $menu );
	$i           = 0;

	foreach ( $menu as $order => $top ) {
		$i++;

		if ( 0 == $order ) { // Dashboard is always shown/single.
			$menu[0][4] = add_cssclass( 'menu-top-first', $top[4] );
			$last_order = 0;
			continue;
		}

		if ( 0 === strpos( $top[2], 'separator' ) && false !== $last_order ) { // If separator.
			$first_item             = true;
			$classes                = $menu[ $last_order ][4];
			$menu[ $last_order ][4] = add_cssclass( 'menu-top-last', $classes );
			continue;
		}

		if ( $first_item ) {
			$classes           = $menu[ $order ][4];
			$menu[ $order ][4] = add_cssclass( 'menu-top-first', $classes );
			$first_item        = false;
		}

		if ( $i == $items_count ) { // Last item.
			$classes           = $menu[ $order ][4];
			$menu[ $order ][4] = add_cssclass( 'menu-top-last', $classes );
		}

		$last_order = $order;
	}

	/**
	 * Filters administration menu array with classes added for top-level items.
	 *
	 * @since 2.7.0
	 *
	 * @param array $menu Associative array of administration menu items.
	 */
	return apply_filters( 'add_menu_classes', $menu );
}

常见问题

FAQs
查看更多 >