Add icons to wordpress menu - php

I have some problems with Wordpress.. I tried to add icons from Wordpress -> Appearance -> Menu and it works but they are not clickable. I also want to remove the text from the menu icon and add it to "data-tooltip". And the last question is: How to add the menu item icons in a different element (not the "li" element) for example "span"?
My code should be:
<ul class="clear-list">
<li>
<span class="crt-icon crt-icon-home"></span>
</li>
<li>
<span class="crt-icon crt-icon-portfolio"></span>
</li>
<li>
<span class="crt-icon crt-icon-references"></span>
</li>
<li>
<span class="crt-icon crt-icon-contact"></span>
</li>
</ul>
But it actually looks like:
<ul id="menu-main-menu" class="clear-list">
<li id="menu-item-60" class="crt-icon crt-icon-home menu-item menu-item-type-post_type menu-item-object-page menu-item-home current-menu-item page_item page-item-7 current_page_item menu-item-60">Home
</li>
<li id="menu-item-55" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-55">Experience
</li>
<li id="menu-item-54" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-54">Portfolio
</li>
<li id="menu-item-58" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-58">Testimonials
</li>
<li id="menu-item-53" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-53">Contacts
</li>
</ul>
I hope you can help me! :)

Add icons: easiest way is to add icons via CSS.
You can to add class for each link in admin and to add icon as pseudo-element, for example if your icon is image:
.crt-icon a:after {
background: url(https://www.iconfinder.com/data/icons/ionicons/512/icon-ios7-contact-512.png);
background-size: cover;
content: '';
display: block;
height: 15px;
width: 15px;
}
If your icon is font-family - you can add it in 'content' CSS rule.
Add tooltip and remove text: you can do it with jquery:
$( '#menu-main-menu a' ).each( function () {
$( this ).attr( 'data-tooltip', $( this ).text() ).empty();
})

Related

Wordpress Navigation Walker change ul, li for div and anchor tags

I am trying to create a custom markup for my WordPress menu, when I add a menu in WordPress, the HTML that shows is as follows:
<div class="menu-menu-container">
<ul id="menu-menu" class="menu">
<li id="menu-item-13" class="menu-item menu-item-type-custom menu-item-object-custom current-menu-item current_page_item menu-item-home menu-item-13">
Home
</li>
<li id="menu-item-14" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-14">
Another Sample
</li>
<li id="menu-item-15" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children menu-item-15">
Sample Page
<ul class="sub-menu">
<li id="menu-item-22" class="menu-item menu-item-type-post_type menu-item-object-post menu-item-22">
Hello world!
</li>
</ul>
</li>
</ul>
</div>
I'm trying to change the code so that it will display using the anchor and div tags instead of ul and li. This is to work with the Bulma framework.
Normal link code:
<a class="navbar-item" href="#">Home</a>
Dropdown link code:
<div class="navbar-item has-dropdown is-hoverable">
<a class="navbar-link" href="#">Sample Page</a>
<div class="navbar-dropdown">
<a class="navbar-item" href="#">Sample Link</a>
</div>
</div>
Another reason I am trying to do this is to remove all of the unnecessary class and id attributes that WordPress automatically adds.
Is this possible with WordPress by using a walker?
Yes it is, you can use
https://github.com/wp-bootstrap/wp-bootstrap-navwalker (most common)
You can also check
1) https://github.com/wp-bootstrap/wp-bootstrap-navlist-walker
2) https://github.com/jprieton/wp-bootstrap4-navwalker
3) https://github.com/dupkey/bs4navwalker

Wordpress Nav - Horizontal Nav with dropdown

I'm trying to create a horizontal nav with a drop down in wordpress.
This is the code I'm tryin to make using the wordpress nav:
<ul>
<li>Home</li>
<li>News</li>
<li class="dropdown">
Dropdown
<div class="dropdown-content">
Link 1
Link 2
Link 3
</div>
</li>
</ul>
What I have for the PHP is this:
<?php $defaults = array(
'container_class' => 'dropdown',
'container_id' => '',
'container' => '',
'menu_class' => 'dropbtn',
'menu_id' => '',
'items_wrap' => '<ul id="%1$s" class="%2$s">%3$s</ul>',
); ?>
<?php wp_nav_menu( $defaults ); ?>
What it puts out is this:
<ul class="dropbtn" id="menu-desktop-menu">
<li class="menu-item menu-item-type-custom menu-item-object-custom current-menu-item current_page_item menu-item-home menu-item-311" id="menu-item-311">Home</li>
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children menu-item-266" id="menu-item-266">Name
<ul class="sub-menu">
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-276" id="menu-item-276">Name</li>
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-274" id="menu-item-274">Name</li>
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-275" id="menu-item-275">Name</li>
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-271" id="menu-item-271">Name</li>
</ul>
</li>
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children menu-item-270" id="menu-item-270">Name
<ul class="sub-menu">
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-277" id="menu-item-277">Name</li>
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-272" id="menu-item-272">Name</li>
</ul>
</li>
</ul>
It's not getting me what I want. I've tried several other things, but I'm really confused on what I need to change to make this work. Can someone help me?
here is simple dropdown menu on hover
.dropbtn > li {
position: relative;
display: block;
height: 40px;
padding: 0 20px;
float: left;
}
.dropbtn .sub-menu {
position: absolute;
top: 100%;
left: 50%;
display: none;
width: 150px;
margin-left: -75px;
padding: 10px;
background-color: #f0f0f0;
}
.dropbtn .sub-menu li {
display: block;
height: 40px;
line-height: 40px;
text-align: center;
border-bottom: 1px dotted #d0d0d0;
}
.dropbtn > li:hover .sub-menu {
display: block;
}
<ul class="dropbtn" id="menu-desktop-menu">
<li class="menu-item menu-item-type-custom menu-item-object-custom current-menu-item current_page_item menu-item-home menu-item-311" id="menu-item-311">Home</li>
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children menu-item-266" id="menu-item-266">Name
<ul class="sub-menu">
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-276" id="menu-item-276">Name</li>
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-274" id="menu-item-274">Name</li>
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-275" id="menu-item-275">Name</li>
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-271" id="menu-item-271">Name</li>
</ul>
</li>
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children menu-item-270" id="menu-item-270">Name
<ul class="sub-menu">
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-277" id="menu-item-277">Name</li>
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-272" id="menu-item-272">Name</li>
</ul>
</li>
</ul>

WordPress: wp_nav_menu sometimes is messed up

I use the wordpress funcion wp_nav_menu() to display a hierarchical menu I created in my WP menu. Here's the snippet:
<?php wp_nav_menu('show_home=1&menu_class=page-navi&sort_column=menu_order'); ?>
Here's its html output:
<div class="menu-default-container"><ul class="page-navi" id="menu-default"><li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-127" id="menu-item-127">Home Page</li>
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children menu-item-128" id="menu-item-128">Il film
<ul class="sub-menu">
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-129" id="menu-item-129">Di che cosa parla?</li>
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-133" id="menu-item-133">Una storia di occasioni</li>
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-130" id="menu-item-130">Il Regista</li>
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-131" id="menu-item-131">Il “maestro”</li>
</ul>
</li>
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-139" id="menu-item-139">Incontri e discussioni</li>
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-153" id="menu-item-153">News ed eventi</li>
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-has-children menu-item-141" id="menu-item-141">Film e lavoro in classe
<ul class="sub-menu">
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-142" id="menu-item-142">Il titolo del film</li>
</ul>
</li>
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-134" id="menu-item-134">Interviste</li>
<li class="menu-item menu-item-type-post_type menu-item-object-page current-menu-item page_item page-item-19 current_page_item menu-item-138" id="menu-item-138">Sponsor e collaborazioni</li>
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-151" id="menu-item-151">Contatti</li>
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-158" id="menu-item-158">Credits</li>
</ul></div>
The problem is that sometimes it's displayed properly:
.. and others isn't:
I checked the source code and it's THE SAME!! :o
Don't know what to think. These things scare me! :P
Here's the website if you want to try to reproduce this (if you're lucky.. in the nights of full moon)..
This is happening because of this style:
ul li {
display: inline;
padding: 2px;
}
Adding the following style to your css should fix your problem:
.menu-default-container ul li {
display: block !important;
}

Regular Expression Function does not match an item

Can someone please help me? I have this php regex function:
preg_replace_callback('/(<li[^>]+class=")([^"]+)("?[^>]+>[^>]+>)([^<]+)<\/a>/')
and I want it to match this string (which is a BootStrap Navbar Group Item):
<li id="menu-item-63" class="fa-home menu-item menu-item-type-custom menu-item-object-custom menu-item-has-children menu-item-63 dropdown open"><a title="Menu" href="#" data-toggle="dropdown" class="dropdown-toggle" aria-haspopup="true">Menu <span class="caret"></span></a>
<ul role="menu" class=" dropdown-menu">
<li id="menu-item-53" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-53"><a title="Multiple Paragraph Page" href="http://localhost:8888/point/?page_id=33">Multiple Paragraph Page</a></li>
<li id="menu-item-52" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-52"><a title="Grandchild Page" href="http://localhost:8888/point/?page_id=40">Grandchild Page</a></li>
<li role="presentation" class="divider"></li>
<li id="menu-item-51" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-51"><a title="Child Page" href="http://localhost:8888/point/?page_id=39">Child Page</a></li>
</ul>
</li>
The function currently works for non group items, like this. But I want it to also include the first LI item above. What the rest of the script then does is, it adds the FontAwesome classes and modifies the Link Text like below:
<li id="menu-item-46" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-46">
<a title="Headers Page" href="http://localhost:8888/point/?page_id=38">
<i class="fa-home fa"></i>
<span class="fontawesome-text"> Headers Page</span>
</a>
</li>
Any help would be much appreciated!
also, here is the complete code from where I got the regex function from: http://pastebin.com/nQttGRnr
I gave up on trying to utilize this script. I figured my problem was that I tried to use the WP Nav Menu Settings Classes, whereas Title Attribute worked without problems!

Jquery Slide down Not working for ul li structure

i have ul li structure like below
<ul class="lft_mnu" id="menu-main-menu">
<li class="menu-item menu-item-type-post_type menu-item-object-page current-menu-item page_item page-item-261 current_page_item menu-item-276" id="menu-item-276">
COMPANY HISTORY
<ul class="sub-menu">
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-287" id="menu-item-287">
Sample 1</li>
<li class="menu-item menu-item-type-post_type menu-item-object-page menu-item-286" id="menu-item-286">
Sample 2</li>
</ul>
</li>
</ul>
i am using below javascript for showing the li sub tab ul with classname sub-menu
<script type="text/javascript">
$('li.menu-item').mousehover(function () {
$('ul.sub-menu').slideDown('medium');
});
$('li.menu-item').mouseleave(function(){
$('ul.sub-menu').slideUp('medium');
});
</script>
i need to show <ul classname="sub-menu"> when i mousehover on <li classname="menu-item"> .
How can i do this?...
Try this:
<script type="text/javascript">
$(function(){
$('li.menu-item ul.sub-menu').hide();
$('li.menu-item').hover(function () {
$('ul.sub-menu', this).slideDown('medium');
}, function(){
$('ul.sub-menu', this).slideUp('medium');
});
});
</script>
One thing that you can change is there is no event like mousehover you need to change it to mouseover. The other thing that you need to note is that your child LI's are also having class menuItem so when the mouse mouses over them there also you will see the sliding behaviour which you might not want. Please check and then you might need to do some more modifications.

Categories