此处的目标是使具有子项的 Wordpress boostrap 主菜单顶级项充当设备 > 768 px 宽的可点击链接,并保留小型设备的默认行为。
在 navwalker.php 文件的第 83 行附近有以下代码:
// If item has_children add atts to a.
if ( $args->has_children && $depth === 0 ) {
$atts['href'] = '#';
$atts['data-toggle'] = 'dropdown';
$atts['class'] = 'dropdown-toggle';
}
为了使链接在普通屏幕上可点击,我需要修改代码,然后添加一个 else 语句,以便它保留默认行为,例如:
// If item has_children add atts to a.
if ( $args->has_children && $depth === 0 ) {
if ( $windowwidth < 768) {
$atts['href'] = '#';
$atts['data-toggle'] = 'dropdown';
$atts['class'] = 'dropdown-toggle';
} else {
$atts['href'] = ! empty( $item->url ) ? $item->url : '';
//$atts['data-toggle'] = 'dropdown';
$atts['class'] = 'dropdown-toggle';
}
}
问题是如何获取 $windowwidth,大概是在 JavaScript 中,以便我可以在 PHP if/else 子句中使用它。