1

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;
     $indent = ( $depth > 0  ? str_repeat( "\t", $depth ) : '' );
     $output .= "\n" . $indent . '<li"' . list item thingys here . '">' . "\n";
  }
}

Any helps highly appreciated.

  • 1
    Hello, Redu - Welcome to the WordPress Development community! Couple quick notes: your desired markup is invalid - all direct descendants of a <ul> must be <li>s in order to conform to the HTML spec. Your description <h2>s should either be moved up a level and placed under the <a> they describe, or placed within their own <li> in the sub-<ul>. Are you certain the code you've posted is as-written on your WP installation? The second line of start_lvl() is missing a single quote. To use the object operator ->, no whitespace should be present on either side, ie $object->property – bosco Feb 5 '15 at 17:57
  • 1
    PHP should have thrown errors for the syntax errors in the posted code - make sure WP_DEBUG is enabled in your wp-config.php such that you're not missing out on any useful errors or warnings. – bosco Feb 5 '15 at 18:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.