I have a nav menu in footer. I just want to display it side by a <p> tag, I mean something like this
<ul> <li> <p>copyright C 2021</p> </li> <li> contact </li> <li> blog </li> </ul>
this is my menu function
if ( function_exists( 'register_nav_menus' ) ) {
register_nav_menus(
array(
'top_nav_menu' => 'My Footer Menu'
)
);
and I’m calling it in footer
<?php if ( has_nav_menu( 'top_nav_menu' ) ) { ?>
<?php wp_nav_menu( array(
'menu' => 'Footer Navigation Menu',
'theme_location' => 'top_nav_menu',
'menu_class' => 'footer-links'
));
?>
<?php } else {?>
<ul class="sf-menu">
<?php wp_list_pages( array('title_li' => '','sort_column' => 'menu_order')); ?>
</ul>
<?php } ?>
How can I insert an extra <li> element just before its first <li> and add a <p> tag between that later added <li> element?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
When using wp_nav_menu() you can exclude the <ul> tag by setting the items_wrap to only include the list items (%3$s), then you can add your own <li> before or after that, and wrap it with <ul> yourself:
<ul class="footer-links">
<li>
<p>copyright C 2021</p>
</li>
<?php
wp_nav_menu(
array(
'menu' => 'Footer Navigation Menu',
'theme_location' => 'top_nav_menu',
'items_wrap' => '%3$s',
)
);
?>
</ul>
Method 2
You can filter the HTML output with the wp_nav_menu filter. Using preg_replace():
add_action( 'wp_nav_menu', 'wpse_385827_filter_nav_menu' );
function wpse_385827_filter_nav_menu( $menu ) {
$menu = preg_replace(
'|<li>|',
'<li><p>Your inserted content here</li><li>',
$menu,
1
);
return $menu;
}
Note: The 1 parameter in the preg_replace() call ensures that only the first <li> tag is affected.
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0