output the post id of wp_nav_menu items
-
I’m a noob wp developer working on my second theme, and have hit a brick wall while trying to hack my way though this.
I’m developing a single-page theme where I want to sniff the page id’s ($post-ID) of the wp_nav_menu items and use those IDs to query the posts that will display on the page.
I realize that a menu item could be a post, a page, a category, or even a tag…but for the sake of simplicity, let’s say they are just pages. (I’m open to other ways to accomplish this too)
I’ve discovered Walker_nav_menu hook and modified it to just output the URLs of menu items:
class pages_from_nav extends Walker_Nav_Menu { function start_el(&$output, $item, $depth, $args) { global $wp_query; $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 ) . '"'; $item_output .= $item->url; $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ); } }
$item->url; outputs the urls ok, but I want to output the IDs of the page for each menu item
$item->ID outputs the ID of the menu item, and not the page ID.
Any help greatly appreciated.
Thanks!
- The topic ‘output the post id of wp_nav_menu items’ is closed to new replies.