jeudi 5 février 2015

How to use a start_el variable in start_lvl


On top of the sub-menu list items i would like to insert a <h2>parent-description</h2> line which i expect to show the description (or the title if wanted) of the parent item. Such as;



<ul>
<li>
<a href="">German Cars</a>
<ul>
<li><a href="">BMW</a></li>
<li><a href="">Mercedes</a></li>
<li><a href="">Volkswagen</a></li>
</ul>
</li>
<li>
<a href="">American Cars</a>
<ul>
<li><a href="">Chrysler</a></li>
<li><a href="">Ford</a></li>
<li><a href="">General Motors</a></li>
</ul>
</li>
</ul>


should look like



<ul>
<li> <!-- $item->description of this list item has to be passed to start_lvl -->
<a href="">German Cars</a>
<ul>
<h2>Cars Made in Germany </h2> <!--parent list item's description shows here-->
<li><a href="">BMW</a></li>
<li><a href="">Mercedes</a></li>
<li><a href="">Volkswagen</a></li>
</ul>
</li>
<li>
<a href="">American Cars</a>
<ul>
<h2>Cars Made in the USA</h2> <!--parent list item's description shows here-->
<li><a href="">Chrysler</a></li>
<li><a href="">Ford</a></li>
<li><a href="">General Motors</a></li>
</ul>
</li>
</ul>


What i know;



  • I have basic knowledge on how to modify the walker by extending to walker_nav_menu.

  • I know that to insert the <h2>parent-description</h2> after the <ul> and before the <li>s i have to concatenate the $output var of the start_lvl method.

  • I know that the $item->description value is avalable within the start_el method.


What i don't know is, how to pass the $item->description value of start_el method (which is used one previously) to the start_lvl method at the time it produces the <ul> tag for list items to follow.


From some previous questions and answers here I implemented something like this but it didn't work.



class My_Nav_Walker extends Walker_Nav_Menu {
private $curItem;

function start_lvl(&$output, $depth = 0, $args = array()) {
$indent = ( $depth > 0 ? str_repeat( "\t", $depth ) : '' );
$output .= "\n" . $indent . '<ul>' . '<h2>' . $this->curItem->description . '</h2>"\n";
}

function start_el(&$output, $item, $depth = 0, $args = array(), $id = 0) {
$this-> curItem = $item;
}
}


Any helps highly appreciated.





Aucun commentaire:

Enregistrer un commentaire