Stripping Out list item code from Wordpress wp_nav_menu

Hi

Really hope I can get some help with this. I have tried to strip out the

  • tags that Wordpress uses for wp_nav_menu

    Using the below code:

    <?php
                                $cleanermenu = wp_nav_menu( array( 
    
                                'theme_location' => 'primary', 
    
                                'container' => false,
    
                                'items_wrap' => '%3$s',
    
                                'echo' => false,
    
                                ) );
    
                                $find = array('><a','<li');
    
                                $replace = array('','<a');
                                
    
                                echo str_replace( $find, $replace, $cleanermenu );
    
                            ?>
    

    But when I view the source code of my page their appears to be an unwanted closing closing list item tag?

    I also tried the below code:

    <?php
                                $cleanermenu = wp_nav_menu( array( 
    
                                'theme_location' => 'primary', 
    
                                'container' => false,
    
                                'items_wrap' => '%3$s',
    
                                'echo' => false,
    
                                ) );
    
                                $find = array('><a','li');
    
                                $replace = array('','a');
    
                                echo str_replace( $find, $replace, $cleanermenu );
    
                            ?>
    

    But this generates and extra closing anchor tag?

    What am I missing, please could somebody help me with this?

    Many Thanks

  • what does $cleanermenu contain when it comes back from the wordpress function?

    Hi

    Sorry not sure I understand, I can send you a PM for a link to the site, would that help?

    Many Thanks

    Well for starters, there wouldnt be a need to PM anything, you’d post it here. But simply put, echo $cleanermenu, copy and paste what it has in it. You’re doing str_replace on a string without knowing what the string is?

    Thanks for the response.

    Do you mean the outputed code?

    If so this is what it returns:

    <a id="menu-item-32" class="features-nav menu-item menu-item-type-post_type menu-item-object-page menu-item-32" href="http:/www.siteaddress.co.uk"></a></a>
    <a id="menu-item-32" class="features-nav menu-item menu-item-type-post_type menu-item-object-page menu-item-32" href="http:/www.siteaddress.co.uk">Features</a></a>
    

    So i’m going to reverse your code process and guess that the original code from the wordpress function was

    <li id="menu-item-32" class="features-nav menu-item menu-item-type-post_type menu-item-object-page menu-item-32"><a href="http:/www.siteaddress.co.uk">Features</a></li>
    

    Which means the str_replace function did exactly what you told it to do.
    Step 1: Replace “><a” with “”.

    <li id="menu-item-32" class="features-nav menu-item menu-item-type-post_type menu-item-object-page menu-item-32" href="http:/www.siteaddress.co.uk">Features</a></li>
    

    Step 2: Replace “li” with “a”.

    <a id="menu-item-32" class="features-nav menu-item menu-item-type-post_type menu-item-object-page menu-item-32" href="http:/www.siteaddress.co.uk">Features</a></a>
    

    What are you missing? Your code didn’t add an extra close tag, you simply didn’t tell it to remove the one that already existed before you began.

    Hi

    Thanks again, yes you are right, I have no idea how I remove the extra closing A tag, what do I need to add to achieve this?

    Many Thanks

    Use the trick of str_replace’s functionality to replace the “</li>” with “” before you do anything else.

    (Hint: str_replace parses each “search” item seperately, FIFO style. There’s a reason i was able to say “step 1” and “step 2” independently in post #6.)

    Also be very careful replacing just “li” as you did in your second attempt. As your URL’s may contain this pattern of characters, and str_replace doesn’t care about context.

    I really do not know what I need to add, I tried a few things, but it either strips out the starting anchor tag or includes another one!

    I will try again!

    Hi

    I am sorry I really cannot get my head around what I need to do, everything I try either adds in a list item, or the ending a tag is duplicated again!

    Thanks

    This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.