Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I am currently using a custom walker to customize the output of wp_nav_menu(), and I am trying to add additional information to the <a> tags.

What I want the output for each menu link to look like is:

<a class="boxPAGEID" href="#">About Me Page</a>

Where PAGEID is the ID of the page I'm linking to.

The reason is because I am developing a theme that opens page content in lightboxes, which are triggered by the class in the tag.

Below is the code of the custom walker in my functions.php file (after the code I'll point to the area where I'm having trouble):

class description_walker extends Walker_Nav_Menu
{

      function start_el(&$output, $item, $depth, $args)
      {
           global $wp_query;     
           $pageid = $wp_query->post->ID;

           $indent = ( $depth ) ? str_repeat( "\t", $depth ) : '';

           $class_names = $value = '';

           $classes = empty( $item->classes ) ? array() : (array) $item->classes;

           $class_names = join( ' ', apply_filters( 'nav_menu_css_class', array_filter( $classes ), $item ) );
           $class_names = ' class="'. esc_attr( $class_names ) . '"';

           $output .= $indent . '<li id="menu-item-'. $item->ID . '"' . $value . $class_names .'>';

           $attributes  = ! empty( $item->attr_title ) ? ' title="'  . esc_attr( $item->attr_title ) .'"' : '';
           $attributes .= ! empty( $item->target )     ? ' target="' . esc_attr( $item->target     ) .'"' : '';
           $attributes .= ! empty( $item->xfn )        ? ' rel="'    . esc_attr( $item->xfn        ) .'"' : '';
           $attributes .= ! empty( $item->url )        ? ' href="'   . '#' .'"' : '';

           $prepend = '<strong>';
           $append = '</strong>';
           $description  = ! empty( $item->description ) ? '<span>'.esc_attr( $item->description ).'</span>' : '';

           if($depth != 0)
           {
                     $description = $append = $prepend = "";
           }

            $item_output = $args->before;
            $item_output .= '<a'. $attributes . 'class="box' . $pageid . '"' .'>';
            $item_output .= $args->link_before .$prepend.apply_filters( 'the_title', $item->title, $item->ID ).$append;
            $item_output .= $args->link_after;
            $item_output .= '</a>';
            $item_output .= $args->after;

            $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args );

            if ($item->menu_order == 1) {
                $classes[] = 'first';
            }

            }
}

Towards the end are a couple of lines that begin with $item_output. The second line is where I'm trying to generate the page ID:

$item_output .= '<a'. $attributes . 'class="box' . $pageid . '"' .'>';

Where $pageid is according to:

global $wp_query;    
$pageid = $wp_query->post->ID;

This gives me a single, fixed ID for all the links generated.

Alternatively, instead of $pageid I tried using $item->ID, but that gave me the ID of the menu item instead.

Any suggestions?

share|improve this question
    
If there are alternatives to the page id, that would work, too. I initially tried using $item->url, but the url wouldn't function as a class name. The page title works only if there are no spaces, and the other attributes are not generated by default. –  Raiman Au Mar 31 '11 at 9:54

2 Answers 2

up vote 11 down vote accepted

The page ID (or object ID, since a menu item can link to any object) is stored in the postmeta table, with the key _menu_item_object_id. So you can get the page ID with the following code:

get_post_meta( $item->ID, '_menu_item_object_id', true );
share|improve this answer
    
JAN!!!!! This did it! Sorry for the late response, but your suggestion did the trick! You're fabulous! Thank you! And for the record for those who aren't sure what to do: I changed the $pageid = $wp_query->post->ID; at the top of the page to $pageid = get_post_meta( $item->ID, '_menu_item_object_id', true ); –  Raiman Au Apr 15 '11 at 4:42
    
@Jan Fabry : Superb!!! it save my time..Great!!! –  Sanjay Khatri Jul 20 '12 at 8:17
    
Brilliant - thanks! –  rico leuthold Mar 15 at 10:56

I could not look at your code deeply but for creating menu maybe you should use get_pages..

http://codex.wordpress.org/Function_Reference/get_pages

<?php 
  $pages = get_pages(); 
  foreach ($pages as $pagg) {
    $option = '<a class="box' . $pagg->ID . '" href="#">';
    $option .= $pagg->post_title;
    $option .= '</a>';
    echo $option;
  }
 ?>
share|improve this answer
    
Thanks for the suggestion, Serdar! Unfortunately this won't work because I need the menu to be able to be controlled from the appearance > menu section of the admin panel! That's why I'm using wp_menu_nav and not get_pages. –  Raiman Au Apr 1 '11 at 4:38

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.