Question

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?

Was it helpful?

Solution

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>

OTHER TIPS

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.

Licensed under: CC-BY-SA with attribution
Not affiliated with wordpress.stackexchange
scroll top