- This topic has 19 replies, 4 voices, and was last updated 3 years, 3 months ago by
Ying.
-
AuthorPosts
-
January 16, 2023 at 2:54 am #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!)
January 16, 2023 at 4:37 am #2497291David
StaffCustomer SupportHi there,
what code did you use to add the image to the menu items ?
January 16, 2023 at 1:27 pm #2498037John
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; }January 17, 2023 at 2:59 pm #2499446John
Any update on this at all please?
January 18, 2023 at 3:45 am #2499931David
StaffCustomer Supportyou 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
subtitleto 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
liis given a class ofcustom-menuif it eitherhas-subtitleorhas-thumbnail
And each item within has amenu-*class to targetCSS 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; }January 19, 2023 at 1:24 am #2501144John
Ok great thank you – let me digest this and get back to you – appreciate your help
January 19, 2023 at 3:31 am #2501260David
StaffCustomer SupportYou’re welcome
January 19, 2023 at 2:48 pm #2502133John
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!January 19, 2023 at 11:43 pm #2502409John
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!January 20, 2023 at 12:04 am #2502424Fernando 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?
January 20, 2023 at 12:28 am #2502448John
No – that puts the subtitle on the same line as the title.
But it does align everything verticallyJanuary 20, 2023 at 1:26 am #2502483Fernando 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; }January 20, 2023 at 1:37 am #2502497John
Wonderful.
That works nicely.
I really do appreciate all of your help and input here.
Thank you so much.January 20, 2023 at 1:58 am #2502522Fernando Customer Support
You’re welcome, John!
January 30, 2023 at 2:02 am #2513784John
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! -
AuthorPosts
- You must be logged in to reply to this topic.