I’m building a horizontal menu and some of the entries on that menu will have dropdowns (sub-menus), and some won’t. Those that have sub-menus are not actually pages. They’re just meant to be guides for the dropdowns.
For instance, say the the horizontal menu is like follows:
Home | About Us | Products | Directions | Contact
And the “products” li item is meant to have 3 pages linked in a vertical dropdown list below it, so “products” itself doesn’t actually represent a page, how can I do that in WP?
(I use WP as a CMS, with static home and inside pages. I build my own templates, style the menus in CSS, then register the menus in the functions.php and call them in the templates.) In WP you add entries to the menus via the list of pages, or by the custom links. But I don’t want “products” to be linked. If I don’t add a link to the custom link it won’t let me add it to the menu.
Is this doable through the menus admin or do I have to approach it some other way?
Thank you for any help!
I have a few ideas:
#
which won’t return anythingThe easiest way to do it without a plugin or anything is to use the “Menus” function of WordPress. Here are the instructions for WordPress 4.8:
The simplest method I came up with was to create a Custom Link item with the Link URL value of
#
. This is sending the user to an empty hash on the same page, so basically links nowhere.However, there are some side-effects of using empty hashes for placeholder links. The link will still appear and behave like a link, so it could confuse a user when they are clicking on what appears to be a link but nothing happens. The other effect is that clicking on an empty hash link will override any existing hash, sending the user to the top of the page. This might not be so worrying for a menu which is at the top of the page anyway, but it is quite jarring when the page unexpectedly jumps when you are not expecting it, especially if this is for a footer menu.
The solution is to combine the empty hash method with a piece of code to detect when empty hash links are used in the menu and to remove the
href
attribute from that link entirely. Ana
element without ahref
attribute is the correct HTML 5 method of creating an placeholder link.This worked for me:
I activated CSS Classes in Menus > Screen Options > CSS Classes
Then I gave the menu element that I wanted to deactivate the class “.nolink”
and added this piece of code to my custom CSS panel:
Using the PHP approach I added this code to functions.php:
This will replace the link with a span element for the item menu with the post_name == “contact”, which is what I was looking for. You can easily change that to check for the menu title or the ID, or add some code to check if it has any child menu items etc.
Create “Custom Links” menu item and add
javascript:;
to the URL field. This is better way than using the#
because it won’t scroll your page to the top when clicked.Appreciate this is an old thread, but for a quick and dirty way of having a link in WordPress is by making the link URL as:
#_
Notice the underscore after the hashtag. This way if your menu scrolls down the page (ie. fixed), you don’t get a jump to the top of the page when clicking on it and doesn’t require any plugins/script.
Add this filter:
Edit span CSS to get same style as
<a>
, don’t forgetcursor: context-menu;
.I solved this way: in header.php (of your theme) I searched for:
and replaced with:
In simple words, this script checks if its parent link ends with “#”, in this case it adds a span element around the content of the A tag, that disables the click.
Hope it helps 🙂
As others here have suggested you can create a custom link menu item with the # as its url. Then erase the # once it is added to the menu. And finally, you can use this simple regex to strip the actual tag from those links.
This will remove the click (and unstyle the item). This way, you dont have to use the custom # links in your menu.
I realize I’m late in the game, but these are the two methods I use:
1) Make the parent menu item a duplicate of the first sub-item, and change its label. For example, if the first item under “Products” is “Product 1”, use “Product 1” as the parent menu item, then change its label to “Products”. That way, both “Products” and “Product 1” will lead to the page Product 1.
2) Add a redirect so that the Products page is redirected to Product 1. The benefit of this option is that it allows you to create a blank Products page to create a hierarchical listing in Pages, but if anyone tries to go to the blank Products page, they will be redirected.
Go to Appearance, then click on menus. In this section go under menu structure and click on the arrow down to expand the page and you’ll see a box that says disable link. Check that box and save.
Writing from 1/2019, the solution that produces proper HTML5 is to do the following.
This will produce a top level nav that is
<a>Menu</a>
which is the correct way to represent a non-clickable link.Create a custom link menu as other as said. View the source code and look for the href of the submenu, for example: #mm-1. Paste this into the URL of the custom link and save the menu. This will make the mobile version work as well when you tap the menu text.
My version looks like this:
first, in the menu in the admin panel in the desired link in the
href
field put#
or leave it empty, and then infunction.php
themes are added:You can replace
'div'
with any element that suits you.This works for version 5.3.2 (tested in this version, but should work in others too).
In this form, the work of all arguments
'before'
,'after'
,'link_before'
,'link_after'
, etc. fromwp_nav_menu()
is preserved.Also, this works for sub-menus too.
Much easier solution can be found on another question:
Admin Menu – Highlight top-level menu when on a sub-menu page (without showing sub-menu)
Look for Askelon’s answer. Work’s perfectly, without needing to do any preg_replaces or jquery.
You may disable the events on the
<a>
tag for all first level menu items using pure css..main-menu
class may have another name according to your menu naming.