• I am trying to add the slug of the linked page as a class on each menu item (for styling purpose).

    I got half way there with this function, which adds the word “slug” to the class. Now I need to replace it with the actual slug of the linked page.

    function add_target_class($output) {
      $output = preg_replace('/class="menu-item/', 'class="slug menu-item', $output);
      return $output;
    }
    add_filter('wp_nav_menu', 'add_target_class');

    Thanks

Viewing 2 replies - 1 through 2 (of 2 total)
Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Add link slug as class to menu item’ is closed to new replies.