Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7999645
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T15:33:48+00:00 2026-06-04T15:33:48+00:00

I have made a function to insert a navigation menu to my WordPress theme.

  • 0

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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T15:33:50+00:00Added an answer on June 4, 2026 at 3:33 pm

    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;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi have made this function which is made to replicate an error that I
I have made a function to cound the weeks in a year, and that
i have made a function to add new upload and description inputs onClick. How
Please help me finding where I have made mistake? delimiter // CREATE FUNCTION `count_photos_in_gallery`(`tmp_gallery_id`
So I have made a little piece of code that will insert and manipulate
I would like to be able to insert a dropdown box where I have
I have just written a stored procedure and stored function that serve to insert
I have made a site with classic php and mysql functions. I order to
I have a problem with a javascript set of functions that I made. This
I have made an application (for my self) for feeds reading, using SyndicationFeed ,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.