Tell me more ×
WordPress Answers is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

Is there an easy way of outputting the current pages menu text?

I am looking for something like

 <?php echo get_the_title(); ?> 

But for the current pages menu text.

share|improve this question
Where are you wanting to output that menu text? Within the page template? Within the Menu Walker? More details please :) – Eric Holmes Mar 12 at 13:33
I plan on outputting it in the widget text, which I made to accept php. I need to work around a cool comercial theme/plugin, so am forced to do ugly things like that. – user1721135 Mar 12 at 13:37
If you're using a commercial theme, start off be creating a child theme (off topic, but best-practice and it will save you if there are any updates in the future) – Eric Holmes Mar 12 at 13:39

1 Answer

up vote 1 down vote accepted

You can get a Menu's entire item list with wp_get_nav_menu_items(). Then, loop through them all and test against the current post_id, and voila, you have all your data.

<?php
$menu_items = wp_get_nav_menu_items( 'main-menu' );
foreach( $menu_items as $item ) {
    print_r( $item ) ; // see what you can work with
    // carry on
}
share|improve this answer
if I paste it like that it should output all items correct? But it doesn't output anythng – user1721135 Mar 12 at 13:46
ah i got the name wrong. – user1721135 Mar 12 at 13:48
I just put in an example menu. The menu name will change depending on the theme you use. – Eric Holmes Mar 12 at 13:50
Now once you see what exactly is in a menu_item object, you can see where the post ID matches up.. chances are it will be in the parent property – Eric Holmes Mar 12 at 13:51
I found this ...WP_Post Object ( [ID] => 74... in the output. So now i need to check against it, like if(current post id = [ID]) display [title] or something like that? – user1721135 Mar 12 at 14:00
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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