0

Hello there i have a navigation menu in my wordpress theme. The navigation menu has two items. One has the label “home” and the other “categories”. I need to rename the label “home” to something else and the “categories” label name as well. How is this possible on the fly?

  • Have you logged into WordPress, in the sidebar go to Appearance > Menus? After you add a link to a menu, you can change the label of the menu item. – ChristopherJones Jul 2 at 13:46
  • I need to change it by code because i have to check for if statement first – stefanosn Jul 2 at 13:47
1

Maybe try filtering just the menu items you want when they get printed back on the front end. You can do that in functions.php

Here you can filter the HTML list content for navigation menus. https://developer.wordpress.org/reference/hooks/wp_nav_menu_items/

After some talk, give this a try and then call your lo() where you need it:

function lo() { 
  add_filter( 'wp_nav_menu_items', 'dynamic_label_change', 10, 2 ); 

  function dynamic_label_change( $items,$args) { 

    if($args->theme_location == 'primary'){ 
      $items = str_replace("Categories", "new_string", $items); 
      $items = str_replace("Latest Offers", "another_new_string", $items); 
    } 
    return $items; 
  } 
} 
  • How do i call this function? – stefanosn Jul 2 at 14:00
  • You can drop this snippet into your functions.php file. – ChristopherJones Jul 2 at 14:04
  • Ok but how the call should be made from another php file? I need to call it whenever i want... – stefanosn Jul 2 at 14:05
  • If you'd like, you can drop this in any theme file really. It is going to filter out the nav items that are printed back from wp_nav_menu(). Which theme file are you trying to work with? – ChristopherJones Jul 2 at 14:07
  • I mean what needs to be passed with the function $items, $args ... – stefanosn Jul 2 at 14:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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