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?
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 edited Apr 5, 2011 at 14:38 Jan Fabry 30.5k4 gold badges90 silver badges136 bronze badges asked Mar 31, 2011 at 9:44 Raiman AuRaiman Au 1531 gold badge1 silver badge6 bronze badges 1- 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 Commented Mar 31, 2011 at 9:54
3 Answers
Reset to default 27The 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 );
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.
I could not look at your code deeply but for creating menu maybe you should use get_pages..
http://codex.wordpress/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;
}
?>