更新:问题实际上是如何包装子菜单,而不是整个菜单。这里有两个选项:
使用jQuery:
<script type="text/javascript">
// run this before the script that sets up the dropdowns:
jQuery(document.ready(function($){
$(\'.sub-menu\').wrap(\'<div class="dropdown"></div>\');
})
</script>
使用PHP 5.3+(选项稍微复杂一些,但我想我会发布它):
<?php
$dom = DOMDocument::loadHTML(wp_nav_menu(array(
\'menu\' => \'main\', // change this to your theme location
\'echo\' => false,
)));
// Find all uls
$uls = $dom->getElementsByTagName(\'ul\');
// Find & wrap each sub menu
foreach ($uls AS $ul)
{
if ($ul->getAttribute(\'class\') == \'sub-menu\')
{
// create the dropdown wrapper div
$wrapper = $dom->createElement(\'div\');
$wrapper->setAttribute(\'class\', \'dropdown\');
// Replace ul with this wrapper div
$ul->parentNode->replaceChild($wrapper, $ul);
// Append this ul to wrapper div
$wrapper->appendChild($ul);
}
}
echo $dom->saveHTML($dom->getElementsByTagName(\'div\')->item(0));
?>
//旧答案:
查看container_class
选项wp_nav_menu()
: http://codex.wordpress.org/Function_Reference/wp_nav_menu
您的菜单ul
将自动包装在div中;可以使用该选项设置div的类:
<?php
wp_nav_menu(array(
\'theme_location\' => \'my_location\', // change this to your theme location
\'container_class\' => \'dropdown\',
));
?>