我有一个左右wp_nav_menu
在前端显示为一个无序列表:
<nav>
<ul>
<?php
// Left menu
wp_nav_menu( array(
\'theme_location\' => \'left\',
\'container\' => false,
\'menu_class\' => false,
\'items_wrap\' => \'%3$s\' // Removes <ul> and wraps <li>
) );
// Right menu
wp_nav_menu( array(
\'theme_location\' => \'right\',
\'container\' => false,
\'menu_class\' => false,
\'items_wrap\' => \'%3$s\' // Removes <ul> and wraps <li>
) );
?>
</ul>
</nav>
在这里重复代码时,我创建了一个名为
$menu
, 像这样:
<nav>
<ul>
<?php
// Left
$menu = array(\'container\' => false, \'menu_class\' => false, \'items_wrap\' => \'%3$s\');
wp_nav_menu( array(
\'theme_location\' => \'left\',
$menu
) );
// Right
wp_nav_menu( array(
\'theme_location\' => \'right\',
$menu
) );
?>
</ul>
</nav>
这是在数组中调用变量的正确方法还是有更有效的方法?
最合适的回答,由SO网友:Jacob Peattie 整理而成
wp_nav_menu()
与此无关。不管怎样,数组都是数组。但不行,这行不通。它将生成如下数组:
array(
\'theme_location\' => \'right\',
array(
\'container\' => false,
\'menu_class\' => false,
\'items_wrap\' => \'%3$s\' // Removes <ul> and wraps <li>
),
)
要获取单个数组中的值,需要将数组与
array_merge()
:
// Left
$menu = array(\'container\' => false, \'menu_class\' => false, \'items_wrap\' => \'%3$s\');
wp_nav_menu( array_merge(
array( \'theme_location\' => \'left\' ),
$menu
) );
// Right
wp_nav_menu( array_merge(
array( \'theme_location\' => \'right\' ),
$menu
) );