为WordPress菜单title属性添加附加文字

WordPress评论366阅读模式

菜单title属性,就是鼠标悬停在菜单标题时的提示文字。通过下面的代码可以为菜单title属性添加附加文字及该菜单的链接。

为WordPress菜单title属性添加附加文字

为WordPress菜单title属性添加附加文字

将代码添加到当前主题函数模板 functions.php 中:

// 为菜单title属性添加附加文字
add_filter( 'walker_nav_menu_start_el', 'zm_nav_menu_item', 20, 4 );
function zm_nav_menu_item( $item_output, $item, $depth, $args ) {
	$extra_title = '附加文字';
	if (strpos( $item_output, 'title=' ) ) {
		// 如果已添加title属性,将$extra_title 附加到现有的菜单title属性。
		return preg_replace( '/title=\"([^\"]*)\"/', 'title="\1 - '.$extra_title.'"', $item_output, 1 );
	} else {
		// 如果未添加title属性,添加菜单标题和$extra_title组成的菜单title属性。
		return str_replace( 'href=', 'title="'.$item->title.' - '. $extra_title.'" href=', $item_output );
	}
}

 

本站文章大部分始于原创,用于个人学习记录,可能对您有所帮助,仅供参考!

weinxin
我的微信
版权声明
本站原创文章转载请注明文章出处及链接,谢谢合作!
 
知更鸟
匿名

发表评论

匿名网友
:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

拖动滑块以完成验证