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?
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:The
PAGEID
is available in$item->object_id
, if$item->object
ispage
.$item->object
contains the type of the menu item. Possible values arepage
,post
,category
,…$item->object_id
contains the ID for the object.I found out this by
var_dump($item)
on WordPress 5.4.2.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