Get the ID of the page a menu item links to?

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:

Read More
<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?

Related posts

Leave a Reply

3 comments

  1. 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 );
    
  2. The PAGEID is available in $item->object_id, if $item->object is page.

    $item->object contains the type of the menu item. Possible values are page,post,category,…

    $item->object_id contains the ID for the object.

    I found out this by var_dump($item) on WordPress 5.4.2.