0

I seek a solution for my navigation in a new WordPress template that I'm developing.

How can I output the unique post ID for every nav item?

I want to create a one-pager (microsite) for a client.

Before I tried something with walker-nav and other things that didn't work.

2
  • if you want it through javascript for client side, you can extract id's from html mark up. Wordpress adds id for each item. Feb 9, 2015 at 9:10
  • mmh, thanks for your reply, i thinked also i can do something with js... but i planned the template in php. i include all post_id's in the microsite content from the navigation in a foreach to check the main nav. so i need the id in a php var.. i can also convert the javascript var in php, but this is not really that quality i wan't to create. is there any other option? Feb 9, 2015 at 9:14

3 Answers 3

0

html mark up for navigation will look like this

<ul id="menu-res" class="main-menu"><li id="menu-item-72" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-72"><a href="#home">Home</a></li>
   <li id="menu-item-238" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-238"><a href="#services">Services</a></li>
   <li id="menu-item-73" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-73"><a href="#about">About</a></li>
   <li id="menu-item-75" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-75"><a href="#skills">Skills</a></li>
   <li id="menu-item-346" class="menu-item menu-item-type-post_type menu-item-object-page menu-item-346"><a href="http://www.yourstorepick.com/pro-site/">Pro Site</a></li>
   <li id="menu-item-347" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-347"><a href="http://www.yourstorepick.com/wp-signup.php?pro-site=1">Sign up</a></li>
   <li id="menu-item-74" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-74"><a href="#our-team">Our Team</a></li>
   <li id="menu-item-239" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-239"><a href="#our-work">Our Work</a></li>
   <li id="menu-item-236" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-236"><a href="#blog">Blog</a></li>
   <li id="menu-item-237" class="menu-item menu-item-type-custom menu-item-object-custom menu-item-237"><a href="#contact">Contact</a></li>
</ul>

From above code you can see that each li has unique ID, in this menu-item-XXX is a format of ID. menu-item- will be same and XXX will vary. You can extract this ID.

2
  • okay, then i think there is no other solutions possible. i will try this now, and give you feedback. thanks for first Feb 9, 2015 at 9:15
  • I haven't tried but this can be done with custom walker class also. But that would be lengthy. Feb 9, 2015 at 9:54
1

If you want to get the corresponding post ID's from a given menu id, try:

$menu_id = 'mymenu'; 
$items = wp_get_nav_menu_items( $menu_id );
$ids = ( $items ) ? wp_list_pluck( $items , 'object_id' ) : array();

where you have to modify the $menu_id to your needs.

0

Soooo, I have tried the solution with javascript. It works without problems.

For debug test:

<script>
    $(function() {
        alert($('.menu-item').attr('id'));
    });
</script>

It gives me the id from every element in the nav out as alert. I extract now the number at the end and have then the menu-item-id.

thanks for all answers, cheers :)

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.