我的函数中有这些行。php修改我的网站前台的一些菜单:
add_filter( \'wp_setup_nav_menu_item\',\'decorate_mobile_menu\' );
function decorate_mobile_menu($item) {
if ( strpos($item->title, \'SHIPPING\') !== false) {
$split = explode(" IN ", $item->title);
$item->title = \'<span class="shipping-menu-item">\' . $split[0] . \' IN </span>\';
$item->title .= \'<br/>\';
$item->title .= \'<span class="shipping-menu-item">\' . $split[1] . \'</span>\';
}
return $item;
}
但每次我在后台“保存”相应的菜单时,它都会再次运行此功能,并且菜单标题中的html变得疯狂:
<span class="shipping-menu-item"><span class="shipping-menu-item"><span class="shipping-menu-item"><span class="shipping-menu-item"><span class="shipping-menu-item">FREE SHIPPING IN </span><br/><span class="shipping-menu-item"></span><br/><span class="shipping-menu-item"></span><br/><span class="shipping-menu-item"></span><br/><span class="shipping-menu-item"></span><br/><span class="shipping-menu-item"><br/>EUROPE FROM 150€</span></span></span></span></span>
你能帮我清理这个功能,只对前台负责吗?
谢谢
最合适的回答,由SO网友:Jacob Peattie 整理而成
把它都包起来! is_admin()
检查:
add_filter( \'wp_setup_nav_menu_item\',\'decorate_mobile_menu\' );
function decorate_mobile_menu($item) {
if ( ! is_admin() ) {
if ( strpos($item->title, \'SHIPPING\') !== false) {
$split = explode(" IN ", $item->title);
$item->title = \'<span class="shipping-menu-item">\' . $split[0] . \' IN </span>\';
$item->title .= \'<br/>\';
$item->title .= \'<span class="shipping-menu-item">\' . $split[1] . \'</span>\';
}
}
return $item;
}
is_admin()
退货
false
如果您正在查看前端
true
如果您正在查看管理区域。