Site logo

[Resolved] Mega Menu Description/second line

Home Forums Support [Resolved] Mega Menu Description/second line

Home Forums Support Mega Menu Description/second line

Viewing 15 posts - 1 through 15 (of 20 total)
  • Author
    Posts
  • #2497167
    John

    Hi there,

    Is it possible to add a second line to Mega Menu names?
    For example, we are using the code you supplied here to create some mega menus for the doctors:
    https://staging.nwhswa.com/
    We would like to add a description like Natural Doctor ND, LAc under each name.
    How can that be achieved?

    I tried multiple snippets and different pieces of code but could not get it to work.

    (FYI – I tried using ChatGTP, but could not get the right answer!)

    #2497291
    David
    Staff
    Customer Support

    Hi there,

    what code did you use to add the image to the menu items ?

    #2498037
    John

    Hello,

    I am not 100% sure wheere I got this, but somewhere from you guys I think.
    It is a snippet added:

    add_filter('wp_nav_menu_objects', 'ad_filter_menu', 10, 2);
    
    function ad_filter_menu($sorted_menu_objects, $args) {
    
        // check for the right menu to filter
        // here we check for the menu with name 'Posts Menu'
        // given that you call it in you theme with:
        //   wp_nav_menu( array( 'menu' => 'Posts Menu' ) );
        // if you call the menu using theme_location, eg:
        //   wp_nav_menu( array( 'theme_location' => 'top_manu' ) );
        // check for:
        //   if ($args->theme_location != 'top_menu')
        if ($args->theme_location === 'primary'){
    		// edit the menu objects
    		foreach ($sorted_menu_objects as $menu_object) {
    			// searching for menu items linking to posts or pages
    			// can add as many post types to the array
    			if ( in_array($menu_object->object, array('post', 'page', 'any_post_type')) && $menu_object->menu_item_parent != 0 ) {
    				// set the title to the post_thumbnail if available
    				// thumbnail size is the second parameter of get_the_post_thumbnail()
    				$thumbnail = '<div class="menu-thumbnail">'.get_the_post_thumbnail($menu_object->object_id, 'thumbnail').'</div>';
    				if (has_post_thumbnail($menu_object->object_id)) {
    					$menu_object->title = $thumbnail.'<span class="menu-item-label">'.$menu_object->title.'</span>';
    				}
    
    			}
    		}
    		
    	}
    	return $sorted_menu_objects;
    
    }
    #2499446
    John

    Any update on this at all please?

    #2499931
    David
    Staff
    Customer Support

    you would need to do something like this instead:

    add_filter('wp_nav_menu_objects', 'ad_filter_menu', 10, 2);
    
    function ad_filter_menu($sorted_menu_objects, $args) {
        // select the menu
        if ($args->theme_location === 'primary'){
            // get the menu objects
            foreach ($sorted_menu_objects as $menu_object) {
                // get any posttype menu item that are not parents
                if ( in_array($menu_object->object, array('post', 'page', 'any_post_type')) && $menu_object->menu_item_parent != 0 ) {
                    // get the custom content 
                    $subtitle = get_post_meta($menu_object->object_id, 'subtitle', true);
                    $thumbnail = get_the_post_thumbnail($menu_object->object_id, 'thumbnail', array( 'class' => 'menu-thumbnail' ));
                    // build the custom classes and menu item
                    if ($subtitle || $thumbnail){
                        $menu_object->classes[] = 'custom-menu';
                        if ($subtitle){$menu_object->classes[] = 'has-subtitle';}
                        if ($thumbnail){$menu_object->classes[] = 'has-thumbnail';}
                        $menu_object->title = sprintf(
                            '%1$s %2$s %3$s',
                            $thumbnail,
                            '<span class="menu-item-label">'.$menu_object->title.'</span>',
                            $subtitle ? '<span class="menu-subtitle">' . $subtitle . '</span>' : ''      
                        );
                    }
                }
            }
        }
        return $sorted_menu_objects;
    }

    The second line will require a custom field on your post.
    This line in the code is where it gets that custom field:

    $subtitle = get_post_meta($menu_object->object_id, 'subtitle', true);

    Change the subtitle to match yours.

    I have tried to make the HTML Conditional so it only prints the html if there is the custom content and add the necessary classes.

    The resulting HTML would look like this:

    <li id="ID" class=".. custom-menu has-subtitle has-thumbnail ...">
        <a href="URL" aria-current="page">
            <img width="150" height="150" src="thumbnail_url" class="menu-thumbnail" alt="" decoding="async" loading="lazy"> 
            <span class="menu-item-label">Title</span> 
            <span class="menu-subtitle">subtitle</span>
        </a>
    </li>

    Note the li is given a class of custom-menu if it either has-subtitle or has-thumbnail
    And each item within has a menu-* class to target

    CSS you can do something like:

    .main-nav ul li.custom-menu a {
        display: flex;
        flex-direction: column;
        flex-wrap: wrap;
        max-height: 60px;
        align-content: flex-start;
        justify-content: center;
    }
    .menu-thumbnail {
        width: 36px;
        border-radius: 100%;
        margin-right: 10px;
    }
    #2501144
    John

    Ok great thank you – let me digest this and get back to you – appreciate your help

    #2501260
    David
    Staff
    Customer Support

    You’re welcome

    #2502133
    John

    Hi David,

    OK – I have added a field using ACF and it is populating as subttile on the required pages.
    I have updated the snippet and the css.
    I can see that something has changed in the menu, but the subtitle field is not loading.

    Any ideas on how to troubleshoot?
    Cheers!

    #2502409
    John

    OK – update – I have it working somewhat now – but I need to adjust something so the space does not appear when there is no subtitle added to the custom field.
    Is that possible?
    SO Basically, if there is no subtitle, the name of the page stays vertically aligned.
    Thank you so much!

    #2502424
    Fernando
    Customer Support

    Hi John,

    If you replace this:

    
    .main-nav ul li.custom-menu a {
        display: flex;
        flex-direction: column;
        flex-wrap: wrap;
        max-height: 60px;
        align-content: flex-start;
        justify-content: center;
    }

    with this:

    .main-nav ul li.custom-menu a:has(.menu-subtitle) {
        display: flex;
        flex-direction: column;
        flex-wrap: wrap;
        max-height: 60px;
        align-content: flex-start;
        justify-content: center;
    }

    does that work?

    #2502448
    John

    No – that puts the subtitle on the same line as the title.
    But it does align everything vertically

    #2502483
    Fernando
    Customer Support

    I see. Instead of the previous one I provided, can you try this:

    .main-nav ul li.custom-menu a {
        display: flex;
        flex-direction: column;
        flex-wrap: wrap;
        height: 60px;
        align-content: flex-start;
        justify-content: center;
    }
    
    .main-nav ul li.custom-menu a img {
        flex: 1 0 100%;
    }
    
    .main-nav ul li.custom-menu a span {
        flex: unset;
    }
    #2502497
    John

    Wonderful.
    That works nicely.
    I really do appreciate all of your help and input here.
    Thank you so much.

    #2502522
    Fernando
    Customer Support

    You’re welcome, John!

    #2513784
    John

    Hi Fernando,

    I have had reports from client and on my wife’s MacBook Pro, that the SUBTITLE is showing on the same line.
    I have cleared all the caches etc etc but still showing.

    I have added !important to this bit of css:
    .main-nav ul li.custom-menu a span {
    flex: unset!important;
    }

    But doesn’t seem to come through.
    ANy ideas?
    Or do you thin kthis is some kind of caching issue?
    Cheers!

Viewing 15 posts - 1 through 15 (of 20 total)
  • You must be logged in to reply to this topic.