-1

This may sound weird, but I need to add different menu items to two specific menu types. When I mean different menu items, I mean, the items I set to main (header) menu are only for that menu and the same thing for footer menu. So, the menu items from primary-menu shouldn't appear on secondary-menu and vice-versa, despite how similar they seem to be.

  add_filter('wp_nav_menu_items', 'register_primary_menu_items', 10, 2);
  if (!function_exists('register_primary_menu_items')) {
    function register_primary_menu_items($items, $args) {
      if ( $args->menu = 'primary-menu' ) {
        $items .= "\n" . "\t\t\t\t\t\t" . '<li><a href="#home">Home</a></li>' . "\n" .
                 "\t\t\t\t\t\t" . '<li><a href="#main">Who are We</a></li>' . "\n" .
                 "\t\t\t\t\t\t" . '<li><a href="#service">Services</a></li>' . "\n" .
                 "\t\t\t\t\t\t" . '<li><a href="#contact">Contacts</a></li>' . "\n";
          //return $items;
      }
    /*
    if ( $args->theme_location = 'secondary-menu' ) {
        $items = '<li><i class="fa fa-angle-right"></i><a href="#home">Home</a></li>' . "\n" .
                 '<li><i class="fa fa-angle-right"></i><a href="#main">Who are We</a></li>' . "\n" .
                 '<li><i class="fa fa-angle-right"></i><a href="#service">Services</a></li>' . "\n" .
                 '<li><i class="fa fa-angle-right"></i><a href="#contact">Contacts</a></li>';
        return $items;
      }*/
        
      return $items;
    }
}

add_filter('wp_nav_menu_items', 'register_secondary_menu_items', 10, 2);
if (!function_exists('register_secondary_menu_items')) {
    function register_secondary_menu_items($items, $args) {
        if ( $args->menu = 'secondary-menu' ) {
            $items = "\n" . "\t\t\t\t\t\t" . '<li><i class="fa fa-angle-right"></i><a href="#home">Home</a></li>' . "\n" .
                     "\t\t\t\t\t\t" . '<li><i class="fa fa-angle-right"></i><a href="#main">Who are We</a></li>' . "\n" .
                     "\t\t\t\t\t\t" . '<li><i class="fa fa-angle-right"></i><a href="#service">Services</a></li>' . "\n" .
                     "\t\t\t\t\t\t" . '<li><i class="fa fa-angle-right"></i><a href="#contact">Contacts</a></li>' . "\n";
            //return $items;
          }
        return $items;
    }
}

That is the code I'm currently using, but it doesn't do what I want. Instead it sets the same menu items from secondary-menu on both header and footer menu or the inverse.

6
  • Is there a reason you’re not just creating the menus in the admin? Dec 1, 2022 at 7:02
  • I'm developing a theme
    – AtomX
    Dec 1, 2022 at 8:09
  • 1
    Themes should support setting the menus from the WordPress admin. Dec 1, 2022 at 8:28
  • I added support to set menus and items from WordPress admin for end user. In this case, I'm not end user. I'm developing and testing stuff. I need to set up menu templates for samples and demos.
    – AtomX
    Dec 1, 2022 at 13:21
  • Well the problem is a basic PHP issue. Your condition is $args->menu = 'primary-menu'. This is setting $args->menu to primary-menu, and then later on you're setting it to secondary-menu. If you want to compare values you need to use ===: php.net/manual/en/language.operators.comparison.php Dec 1, 2022 at 13:25

1 Answer 1

0

So, what was messing up with the menu items setting was assignment operator on the decision structure (condition) instead of comparison (or equal) operator to add the items based on menu location. I don't know how I missed or made that shameful kind of mistake.

Anyway, below is the working code template for WordPress menu editor in general that meets my needs. I had to replace menu argument with theme_location, menu wasn't working for me (Don't know why yet, and I doubt I have time to try to figure it out anyway).

add_filter('wp_nav_menu_items', 'register_header_footer_menu_items', 10, 2);
  if (!function_exists('register_header_footer_menu_items')) {
    function register_header_footer_menu_items($items, $args) {
      if ( $args->theme_location == 'primary-menu' ) {
        $items .= "\n" . "\t\t\t\t\t\t" . '<li><a href="#home">Home</a></li>' . "\n" .
                 "\t\t\t\t\t\t" . '<li><a href="#main">Who are We</a></li>' . "\n" .
                 "\t\t\t\t\t\t" . '<li><a href="#service">Services</a></li>' . "\n" .
                 "\t\t\t\t\t\t" . '<li><a href="#contact">Contacts</a></li>' . "\n";
          return $items;
      }
        
        if ( $args->theme_location == 'secondary-menu' ) {
            $items .= "\n" . "\t\t\t\t\t\t" . '<li><i class="fa fa-angle-right"></i><a href="#home">Home</a></li>' . "\n" .
                     "\t\t\t\t\t\t" . '<li><i class="fa fa-angle-right"></i><a href="#main">Who are We</a></li>' . "\n" .
                     "\t\t\t\t\t\t" . '<li><i class="fa fa-angle-right"></i><a href="#service">Services</a></li>' . "\n" .
                     "\t\t\t\t\t\t" . '<li><i class="fa fa-angle-right"></i><a href="#contact">Contacts</a></li>' . "\n";
            return $items;
          }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.