问题描述
我制作了一个功能,可以将导航菜单插入到我的 Wordpress 主题中.我想听听你的建议,如何让它更好,也许更小.这是:
I have made a function to insert a navigation menu to my Wordpress theme. Iwould like your suggestions on how to make it better and perhaps smaller. Here it is:
function cosmos_nav_menu($theme_location, $type) {
switch ( $type ) {
case 'basic-tabs' :
$menu_class = 'nav nav-tabs';
$wrap_before = '';
$wrap_after = '';
break;
case 'stacked-tabs' :
$menu_class = 'nav nav-tabs nav-stacked';
$wrap_before = '';
$wrap_after = '';
break;
case 'basic-pills' :
$menu_class = 'nav nav-pills';
$wrap_before = '';
$wrap_after = '';
break;
case 'stacked-pills' :
$menu_class = 'nav nav-pills nav-stacked';
$wrap_before = '';
$wrap_after = '';
break;
case 'list' :
$menu_class = 'nav nav-list';
$wrap_before = '';
$wrap_after = '';
break;
case 'navbar' :
$menu_class = 'nav';
$wrap_before = "<div class=\"navbar\">\n<div class=\"navbar-inner\">\n<div class=\"container\">\n";
$wrap_before .= "<a class=\"btn btn-navbar\" data-toggle=\"collapse\" data-target=\".nav-collapse\">\n";
$wrap_before .= "<span class=\"icon-bar\"></span>\n<span class=\"icon-bar\"></span>\n<span class=\"icon-bar\"></span>\n</a>\n";
$wrap_before .= "<div class=\"nav-collapse\">\n";
$wrap_after = "\n</div>\n</div>\n</div>\n</div>";
break;
case 'navbar-brand' :
$menu_class = 'nav';
$wrap_before = "<div class=\"navbar\">\n<div class=\"navbar-inner\">\n<div class=\"container\">\n";
$wrap_before .= "<a class=\"btn btn-navbar\" data-toggle=\"collapse\" data-target=\".nav-collapse\">\n";
$wrap_before .= "<span class=\"icon-bar\"></span>\n<span class=\"icon-bar\"></span>\n<span class=\"icon-bar\"></span>\n</a>\n";
$wrap_before .= "<a class=\"brand\" href=\"" . home_url('/') . "\">" . esc_html(get_bloginfo('name', 'display')) . "</a>\n";
$wrap_before .= "<div class=\"nav-collapse\">\n";
$wrap_after = "\n</div>\n</div>\n</div>\n</div>";
break;
}
echo $wrap_before;
wp_nav_menu(array(
'theme_location' => $theme_location,
'container' => false,
'menu_class' => $menu_class,
'walker' => new Bootstrap_Walker()
));
echo $wrap_after;
}
它基本上是 wordpress 的 wp_nav_menu() 函数的包装器.它在基于 $type 变量调用 wp_nav_menu() 函数之前和之后添加了一些 html 代码.前 5 个案例的前后代码都没有.在最后两种情况下也有相当多的代码重复.我还想包括一个情况,其中提供给 $type 变量的字符串不匹配任何情况并根据回显消息.
It's basically a wrapper around wordpress' wp_nav_menu() function. It adds some html code before and after calling the wp_nav_menu() function based on the $type variable. The before and after code for the first 5 cases in none. And there's also quite a bit of code duplication in the last 2 cases. I would like also to include a case where the string given to $type variable doesn't match any of the cases and echo a message according.
我最初用很多if"语句而不是case"来编码这个函数,但它们中的任何一个对我来说都不好看.
I initially coded this function with a lot of "if" statements instead of "case", but either of them did't look nice to me.
任何帮助将不胜感激.
推荐答案
我会设置默认的 wrap_before 和 wrap_after,比如:
I would set the default wrap_before and wrap_after, something like:
$wrap_before = '';
$wrap_after = '';
switch ($type)
{
case 'basic-tabs' :
$menu_class = 'nav nav-tabs';
break;
case 'basic-tabs' :
$menu_class = 'nav nav-tabs nav-stacked';
break;
}
这篇关于wordpress导航菜单的php代码优化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!