WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] remove title="whatever" from wp_nav_menu (6 posts)

  1. inspirationally
    Member
    Posted 3 years ago #

    Hello,

    I try removing the title from the top menu, but it doesn't work. i mean the

    <a href="http://bla.com/news/">title="news"news</a>.
    I need it, because I want to use a qTip on all links, but in the top navi it looks horrible when there's also the submenu appearing.

    I tried the following code in my functions.php

    //removes title from top menu
    
    function my_nav_notitle( $menu ){
      return preg_replace('title=\"(.*?)\"','', '', $menu );
    }
    add_filter( 'wp_nav_menu', 'my_nav_notitle' );

    and also altered the title=\"... line a bit, but could not find anything that works.

    Can you help, please?

    Thank you!!

  2. Mark / t31os
    Moderator
    Posted 3 years ago #

    http://php.net/manual/en/function.preg-replace.php

    Your args are out of place and your first arg is missing delimeters..

    preg_replace('| title=\"(.*?)\"|', '', $menu );

    See if that helps... ;)

  3. inspirationally
    Member
    Posted 3 years ago #

    Hey Mark,

    thanks for your answer, sadly it doesn't do the trick, even after cache purging.
    And even with wrapping the wp_nav_menu function into the my_nav_notitle.
    It simply doesn't work.

    Just for testing, I also tried this http://wordpress.stackexchange.com/questions/2574/remove-wrapping-div-and-ul-from-output-of-wp-nav-menu
    solution to remove the <div>

      before the navigation, but it also doesn't work.

      This is how I include the navi into the header.php with one of the functions for the case the add_filter part doesn't work:

      <?php my_nav_unlister(wp_nav_menu( array( 'container_class' => 'menu-header', 'theme_location' => 'primary', 'depth' => '2' )) ); ?>

      with the linked function.

      and even if i write

      <?php preg_replace( array( '#^<ul[^>]*>#', '#</ul>
      $#' ), '', (wp_nav_menu( array( 'container_class' => 'menu-header', 'theme_location' => 'primary', 'depth' => '2' )) )); ?>

      or

      <?php preg_replace('| title=\"(.*?)\"|', '', (wp_nav_menu( array( 'container_class' => 'menu-header', 'theme_location' => 'primary', 'depth' => '2' )) )); ?>

      it doesn't want to change.

      Is it possible, the mistake is (also) elsewhere?

  4. Mark / t31os
    Moderator
    Posted 3 years ago #

    This works just fine for me..

    function my_nav_notitle( $menu ){
      return $menu = preg_replace('|title=\"(.+)\"|', '', $menu );
    }
    add_filter( 'wp_nav_menu', 'my_nav_notitle' );

    Just make sure you've assigned a menu to your registred nav menu location, else it'll render a page menu(the fallback) in place, which won't adhere to the filter(since it's a different kind of menu)..

  5. inspirationally
    Member
    Posted 3 years ago #

    Thanks again, Mark. I did not know wp_nav_menu would become wp_page_menu automatically. So after changing my code to wp_page_menu and

    function my_nav_notitle( $menu ){
      return $menu = preg_replace('/ title=\"(.*?)\"/', '', $menu );
    
    }
    add_filter( 'wp_page_menu', 'my_nav_notitle' );

    it did work at the end.

  6. Mark / t31os
    Moderator
    Posted 3 years ago #

    Happy i could help... :)

Topic Closed

This topic has been closed to new replies.

About this Topic