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="#" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener">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="' . '#' .'" rel="nofollow noreferrer noopener"' : '';
$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?
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 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 );
Method 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.
Method 3
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="#" rel="nofollow noreferrer noopener" rel="nofollow noreferrer noopener">';
$option .= $pagg->post_title;
$option .= '</a>';
echo $option;
}
?>
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0