I search on this site and found many answers for this question. Most of them is not working on my theme.
Here is a one solution I found and it’s working according to my need.
function wp_nav_menu_no_ul()
{
$options = array(
'echo' => false,
'container' => false,
'theme_location' => 'primary'
);
$menu = wp_nav_menu($options);
echo preg_replace(array(
'#^<ul[^>]*>#',
'#</ul>$#'
), '', $menu);
}
This code will remove ul
at beginning and the end of wp_nav_menu()
. So in my theme I just write
<ul class="primary-nav">
<?php wp_nav_menu_no_ul(); ?>
</ul>
But the problem is coming again when I do not add or activate any menu via admin. http://domain.com/wp-admin/nav-menus.php
Question :
How do I remove the <div><ul>**</ul></div>
whether the menu is active or not. Let me know
Finally I got it worked 🙂 functions.php
function wp_nav_menu_no_ul()
{
$options = array(
'echo' => false,
'container' => false,
'theme_location' => 'primary',
'fallback_cb'=> 'default_page_menu'
);
$menu = wp_nav_menu($options);
echo preg_replace(array(
'#^<ul[^>]*>#',
'#</ul>$#'
), '', $menu);
}
function default_page_menu() {
wp_list_pages('title_li=');
}
header.php
<ul class="primary-nav">
<?php wp_nav_menu_no_ul(); ?>
</ul>
The function wp_nav_menu takes an argument of fallback_cb which is the name of the function to run if the menu doesn’t exist.
so change you code to something like this:
you can even remove the container from the menu and do other stuff with some more arguments sent to the wp_nav_menu function
Hope this helps.
Actually, WordPress supports this by default:
The default for
items_wrap
is<ul id="%1$s" class="%2$s">%3$s</ul>
.The below code should simple do it.
Reference this link for the wp_nav_menu function http://codex.wordpress.org/Function_Reference/wp_nav_menu
If you want to print only
<a>
tags, you may go this way:For me what worked was this:
Hope it helps.
I know that this answer is not completely for this question but there are so many people who come to know how to remove ul and li tag in WordPress and add another tag in WordPress.
Like before applying my code WordPress gives these types of output in menu
But someone wants to change ul into div and li into a tag then you should use below code
This gives output in the following format