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!
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It only takes a minute to sign up.
Sign up to join this communityThis 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.
add_filter( 'wp_nav_menu_objects', 'wpse16243_wp_nav_menu_objects' );
function wpse16243_wp_nav_menu_objects( $sorted_menu_items )
{
foreach ( $sorted_menu_items as $menu_item ) {
if ( $menu_item->current ) {
$GLOBALS['wpse16243_title'] = $menu_item->title;
break;
}
}
return $sorted_menu_items;
}
You can now use this new global variable instead of the normal title. Simple example:
add_filter( 'single_cat_title', 'wpse16243_single_cat_title' );
function wpse16243_single_cat_title( $cat_title )
{
if ( isset( $GLOBALS['wpse16243_title'] ) ) {
return $GLOBALS['wpse16243_title'];
}
return $cat_title;
}
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.
wpse16243_wp_nav_menu_objects
to retrieve current menu ID. But it's not consistant. Switching between home page and a menu item does nto always give me correct menu item - it kind of "lags". Any suggestion on how I can retrieve current menu ID?
wpse16243
? I see this strange type of name fairly common and wondering what it's all about. Disclaimer: Wordpress noob.
Jan 28, 2012 at 15:32
WordPress Stack Exchange question number 16243
. If people directly copy this code, it won't conflict with other plugins that might share the same function names.
Jan 29, 2012 at 10:47
You can use wp_get_nav_menu_items()
. Here's a sample function
:
<?php
function my_get_menu_item_name( $loc ) {
global $post;
$locs = get_nav_menu_locations();
$menu = wp_get_nav_menu_object( $locs[$loc] );
if($menu) {
$items = wp_get_nav_menu_items($menu->term_id);
foreach ($items as $k => $v) {
// Check if this menu item links to the current page
if ($items[$k]->object_id == $post->ID) {
$name = $items[$k]->title;
break;
}
}
}
return $name;
}
Then call the function
with the location name of the nav menu you wish to use:
<?php
$menu_name = my_get_menu_item_name( 'NAV_MENU_LOCATION_NAME' ); // eg. 'primary'
echo $menu_name;
add_filter( 'wp_nav_menu_objects', 'wpse16243_wp_nav_menu_objects' );
function wpse16243_wp_nav_menu_objects( $sorted_menu_items )
{
foreach ( $sorted_menu_items as $menu_item ) {
if ($menu_item->current ) {
$GLOBALS['currentMenuTitle'] = $menu_item->title;
$GLOBALS['currentMenuID'] = $menu_item->ID;
break;
}
}
return $sorted_menu_items;
}
function get_menu_items_children( $menu_item_id='' )
{
global $wpdb;
// AND meta_key='_menu_item_menu_item_parent'
$myrows = $wpdb->get_results( "SELECT * FROM $wpdb->postmeta JOIN $wpdb->posts ON $wpdb->postmeta.post_id=$wpdb->posts.id WHERE meta_value = '".$menu_item_id."' AND meta_key='_menu_item_menu_item_parent' ORDER BY $wpdb->posts.menu_order ASC" );
foreach ( $myrows as $menu_item ) {
$sc = get_object_vars($menu_item);
$myrowsb = $wpdb->get_results( "SELECT * FROM $wpdb->postmeta WHERE post_id = '".$sc['post_id']."' AND meta_key='_menu_item_object_id' " );
foreach ( $myrowsb as $menu_itemb ) {
$scb = get_object_vars($menu_itemb);
$pmIDs[] = $scb['meta_value'];
}
}
return $pmIDs;
}
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:
$cat = get_the_category();
$currentcat = $cat[0];
Which makes available the following variables:
$currentcat=>cat_ID // Category ID
$currentcat=>cat_name // Category Name (Same as Title)
$currentcat=>category_nicename // Category Slug
$currentcat=>category_description // Category Description
$currentcat=>category_count // Category Count (# of Posts w/ this Category)
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.
wp_get_nav_menu_items()
.
May 3, 2011 at 16:11