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!
-
bump, any takers?
After some more research getting me nowhere on the issue, I’m wondering If the only way to accomplish this is via a custom sql query.
I’ve been avoiding that because there must be a better way, after all $item->url figures out how to build the link based on the destination content, but perhaps there’s not.
Is there a way to hook into the $url variable and get the content id that way?
Maybe you can use $item->object_id;
That totally did it! Thanks a million.
BTW, do you know of a reference of where I could see a list of variables like object_id to know that it exists?
Here’s the Walker_Nav_Menu code that I have working:
It gets the main menu and outputs each menu item’s post-id in a comma delimited list.
// get menu item's content id class pages_from_nav extends Walker_Nav_Menu { function start_el(&$output, $item, $depth, $args) { global $wp_query; $item_output .= $item->object_id; $item_output .= ','; $output .= apply_filters( 'walker_nav_menu_start_el', $item_output, $item, $depth, $args ); } }
a few days ago I had the same problem with you, and make my hair fall out faster 🙂
Then I tried to see wp-includes/nav-menu.php, and found $menu_item-> object_id, and I tried it, and succeeded.Sorry for my bad english
$item->object_id – really helped much!
That looks like just what i want but i paste in the code and get WSOD in functions.php and a page template.
I’m looking for a way to pass the menu order over to an instance of wp-dTree so a list of IDs might do it.
oops i had it in both!
So, how do you call this?
I an somewhat dumb, sorry..
- The topic ‘output the post id of wp_nav_menu items’ is closed to new replies.