Navigation
This is archived content. Visit our new forum.

Tagged: 

  • Author
    Posts
  • #1062961

    RickyWolff
    Participant
    #1062964

    RickyWolff
    Participant

    Excuse me, the above html got messed up. Here is the correct Snippet

    <nav class="x-nav-wrap desktop" role="navigation">
      <ul id="menu-about-me-menu" class="x-nav"><li id="menu-item-583" class="tst menu-item menu-item-type-post_type menu-item-object-page current-menu-item page_item page-item-498 current_page_item menu-item-583"><a class="google-tag-class" href="http://rickywolff.com/"><span>Home</span></a></li>
    #1063024

    RickyWolff
    Participant

    I found a solution. You can mark this threat as solved 🙂

    The answer is adding the below code to your functions.php file.

    function add_menuclass($ulclass) {
       return preg_replace('/<a /', '<a class="list-group-item"', $ulclass);
    }
    add_filter('wp_nav_menu','add_menuclass');

    If you have code to enrich this code snippet I’d love to hear.

    Regards,

    Ricky

    #1063031

    Friech
    Moderator

    Hi Ricky,

    Thanks for writing in and I’m glad you sorted things out. However, WordPress has a native option for you to assign a class to your menu items.


    screenshot

    If you don’t see this class field on your end. Press the Screen Options on the upper-right corner and enable the CSS Classes.


    screenshot

    Hope it helps, Cheers!

    #1063032

    RickyWolff
    Participant

    Hmm the above code removes the ability to use the search function in the navbar. Any ideas on how to fix this?

    #1063038

    RickyWolff
    Participant

    Hi Friech,

    I have tried that, but the css classes didn’t seem to appear in my menu. Like the settings aren’t broadcasting to the website. Do you know why?

    -edit: The optional CSS classes do show in WordPress as you displayed, but they don’t appear in the source code of my live website.

    #1063116

    Christopher
    Moderator

    Thanks for writing in! To assist you with this issue, we’ll first need you to provide us with your URL. This is to ensure that we can provide you with a tailored answer to your situation. Once you have provided us with your URL, we will be happy to assist you with everything.

    #1064597

    RickyWolff
    Participant

    Excuse my delayed reply, my website url is http://rickywolff.com

    #1064716

    Christopher
    Moderator

    Hi there,

    The class name added successfully, please see the attachment.

    Thanks.

    #1064721

    RickyWolff
    Participant

    Edit: I am trying to get the class onto the < a> tag, not on the < li> tag. Is this possible? The code I provided above worked, but that broke my search button in my nav menu

    #1064795

    RickyWolff
    Participant

    It’s finally solved. Adding this to my functions.php did the trick:

    
    function my_walker_nav_menu_start_el($item_output, $item, $depth, $args) {
        $classes     = implode(' ', $item->classes);
        $item_output = preg_replace('/<a /', '<a class="'.$classes.'"', $item_output, 1);
        return $item_output;
     }
    add_filter('walker_nav_menu_start_el', 'my_walker_nav_menu_start_el', 10, 4);

    Thanks for your efforts support!

    Kind regards,

    Ricky

    #1064808

    Friech
    Moderator

    Glad you sorted things out. Should you require any further assistance with X|THEME and Cornerstone, please feel free to get in touch with us.

    Cheers!