WordPress.org

Ready to get started?Download WordPress

Forums

Add link slug as class to menu item (3 posts)

  1. Eric3D
    Member
    Posted 2 years ago #

    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

  2. benklocek
    Member
    Posted 2 years ago #

  3. Eric3D
    Member
    Posted 2 years ago #

    Thanks. This is exactly what I was looking for.

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags