1。CSS::first-letter
第一个选项是
selector CSS ::first-letter
.
li.menu-item.active a::first-letter {
color: red;
}
The
a
菜单内的标签应具有
display: block
/
display: inline-block
.
Note: ::第一个字母选择器只能用于块级元素。
2。滤器nav_menu_item_title
另一种方法是使用
nav_menu_item_title
过滤第一个字母并用CSS类标记将其包围。
add_filter( \'nav_menu_item_title\', \'se337090_first_letter\', 30, 4 );
function se337090_first_letter( $title, $item, $args, $depth )
{
if ( $item->current )
{
//
// after application of this filter, there is the concatenation:
// $item_output .= $args->link_before . $title . $args->link_after;
$first_letter = substr( $title, 0, 1);
$title = substr($title, 1);
$args->link_before .= sprintf( \'<span class="menu-item-first-letter">%s</span>\', $first_letter );
// but you can also add changes to the title:
// $title_end = substr($title, 1);
// $title = sprintf( \'<span class="menu-item-first-letter">%s</span>%s\', $first_letter, $title_end )
}
return $title;
}