Skip to content

Instantly share code, notes, and snippets.

@fjaguero
Created October 17, 2012 12:33
Show Gist options
  • Save fjaguero/3905304 to your computer and use it in GitHub Desktop.
Save fjaguero/3905304 to your computer and use it in GitHub Desktop.
PHP: Show the title of a menu element depending the current category
/* ----------------------------- FUNCTIONS.PHP --------------------------*/
/* @Workether ------------------------------------------------------------
Get Category ID by his name.
--------------------------------------------------------------------------*/
function get_category_id($cat_name){
$term = get_term_by('name', $cat_name, 'category');
return $term->term_id;
}
/* ----------------------------- END FUNCTIONS.PHP --------------------------*/
<?php
/* @Workether ------------------------------------------------------------
Get the first current category
--------------------------------------------------------------------------*/
$category = get_the_category();
?>
<div id="blog-header">
<h1>
<?php
$menu_titles = wp_get_nav_menu_items(38); // Get menu ID=38
$category_ID = get_category_id($category[0]->cat_name); // Get the first category ID via the get_category_id custom function
switch ($category_ID)
{
case '10':
// If the ID is 10, get the first element of the menu
$title = $menu_titles[0]->title;
echo $title;
break;
case '11':
// If the ID is 11, get the secont element of the menu
$title = $menu_titles[1]->title;
echo $title;
break;
case '3':
// If the ID is 3, get the third element of the menu
$title = $menu_titles[2]->title;
echo $title;
break;
// For every other element not listed above
default:
echo "Latest Posts";
}
?>
</h1>
</div>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment