php code optimization for wordpress nav menu

2019-08-07 11:05发布

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;
}

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.

I initially coded this function with a lot of "if" statements instead of "case", but either of them did't look nice to me.

Any help will be appreciated.

1条回答
一纸荒年 Trace。
2楼-- · 2019-08-07 11:48

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;
}
查看更多
登录 后发表回答