Is possible to get current-menu-item as a php variable?
I’m listing the category museums via the menu item ‘museums we support’ so i’d like the get ‘museums we support’ bit and display it somewhere?
Any help appreciated!
Is possible to get current-menu-item as a php variable?
I’m listing the category museums via the menu item ‘museums we support’ so i’d like the get ‘museums we support’ bit and display it somewhere?
Any help appreciated!
You must be logged in to post a comment.
This is possible by filtering
wp_nav_menu_objects
, which is the easiest place to check which item is the current menu item, because WordPress already added the classes for you.You can now use this new global variable instead of the normal title. Simple example:
Of course, this only works if you display the menu before you display the title. If you need it earlier (maybe in the
<title>
element?), you should first render the menu and then display it later.You can use
wp_get_nav_menu_items()
. Here’s a samplefunction
:Then call the
function
with the location name of the nav menu you wish to use:I’m not sure I exactly follow.
The “Museums We Support” is generated by a Post Title, Page Title, Category Title, etc. (or, if you’re using a Custom Navigation Menu, it could be a custom Title).
What are you trying to do with this text? Where are you trying to display it? I assume you’re trying to display it on the Category Index Page (since you indicate it is
current-menu-item
)? If so, simply call<?php single_cat_title(); ?>
to output the Category Title.If you need additional category information, you can use something like:
Which makes available the following variables:
If you’re somewhere other than the Category Index Page, you’ll need to pass the $catid to these functions.
EDIT:
Since you indicated that “Museums We Support” is a custom Menu Title, the easiest approach would be to rename the Category from “Museums” to “Museums We Support” (note: you can leave the slug,
museums
, unchanged). However, doing so would mean that “Museums We Support” would be displayed as the Category Title wherever else it might be output in your template. If that is acceptable, then no worries.Otherwise, you will have to use something like
wp_get_nav_menu_items()
(Codex ref) in order to grab the Title for the specific menu item.