php - str_replace wordpress menu -
i have menu of following hyperlinks.
1) our work 2) clients 3) 4) contact
my question is, how can prevent following code not replace li
in 'clients'?
it outputs 'caents', although need stop outputting <li>
tags in menu.
<nav> <?php // first let's our nav menu using regular wp_nav_menu() function special parameters $cleanmenu = wp_nav_menu( array( 'container' => false, // div outside menu ul, don't need 'depth' => 0, 'sort_column' => 'menu_order, post_title', 'echo' => false, // don't display yet ) ); // find closing bracket of each li , opening of link, instances of "li" $find = array('><a','li'); // replace former nothing (a.k.a. delete) , latter "a" $replace = array('','a'); echo str_replace( $find, $replace, $cleanmenu ); ?> </nav>
i wouldn't recommend parsing html way. take time , learn php dom extension. it's quite flexible. here's example want , illustrates basic methods parsing html php:
<nav> <?php $cleanmenu = wp_nav_menu( array( 'container' => false, 'depth' => 0, 'sort_column' => 'menu_order, post_title', 'echo' => false, ) ); $html = ''; libxml_use_internal_errors(true); $dom = new domdocument(); $dom->preservewhitespace = false; $dom->loadhtml($cleanmenu); $listitems = $dom->getelementsbytagname('li'); foreach($listitems $li) { $id = $li->getattribute('id'); $class = $li->getattribute('class'); $href = $li->getelementsbytagname('a')->item(0)->getattribute('href'); $text = $li->getelementsbytagname('a')->item(0)->nodevalue; $html .= '<a id="' . $id . '" class="' . $class . '" href="' . $href . '">' . $text . '</a>'.php_eol; } echo $html; ?> </nav>
Comments
Post a Comment