I know how to get the Current Page Title, also I know how to get the Menu Active Page Title but I need to actually get the 2 parent Active Menu via PHP.
Active Menu Title
$app = JFactory::getApplication();
$menu = $app->getMenu();
$title = $menu->getActive()->title;
Menu Example
1st Level - CARS
2nd Level - FERRARI
3rd Level - F50
If I am at the 3 level menu F50 how can I get the CARS active menu title?
A method, i used this:
//import to faster development
jimport( 'joomla.application.categories' );
$options = array();
$categories = JCategories::getInstance('Content', $options);
$category = $categories->get($this->category->id);
$parent = $category->getParent();
And here is the magic
//here you get the grandparent category
$grandtemp = $categories->get($parent->id);
$grandparent = $grandtemp->getParent();
//print the values of parent of parent (1st level)
print_r($grandparent)
if you have any question you can comment
Here you go:
$app = JFactory::getApplication();
$menu = $app->getMenu();
$baseId = $menu->getActive($params)->tree[0]; //tree[1] for the second parent etc
//query the database to find the title
$db = JFactory::getDBO();
$query = "SELECT title FROM #__menu WHERE id=".$baseId;
$db->setQuery( $query );
$topParentTitle = $db->loadResult();
Related
I am trying to get 2nd level category of a single Post.
A Post is under this hierarchy Sports > Football > premier league
i want to get the ID for Sport how can i do this.
Thank You.
You can get all first level categories by this code for particular post
$cat = get_the_category($post_id);
then by this one you can get child categories
$child_categories=get_categories(
array( 'parent' => $cat->cat_ID )
);
Try this, this will work for 'nth' level
$category = get_the_category();
$parent = get_ancestors($category[0]->term_id,'category');
if (empty($parent)) {
$parent[] = array($category[0]->term_id);
}
$parent = array_pop($parent);
$parent = get_category($parent);
if (!is_wp_error($parent)) {
var_dump($parent);
} else {
echo $parent->get_error_message();
}
Reference:
get_the_category
get_ancestors
Currently I'm using the following two-part solution to get the post's top level category ID.
In functions.php:
function get_top_level_cat_id ($catid) {
while ($catid) {
$cat = get_category($catid);
$catid = $cat->category_parent;
$catparent = $cat->cat_ID; }
return $catparent; }
In single.php:
$category = get_the_category($post->ID);
$catid = $category[0]->cat_ID;
$top_level_cat_id = get_top_level_cat_id ($catid);
if ($top_level_cat_id == 1) {$type_it = "Top Level Category ID is 1";}
else {$type_it = "Top Level Category ID is not 1";}
echo $type_it;
It works, but using functions.php is not a good decision in my case. Putting the function, the way it should reside in functions.php, into single.php predictably ruins the output.
Is there any solution that will allow to skip using functions.php while outputting top level category ID of the current post?
I have some menu-items published on footer of my site.
-FAQ
-Contact Us
These are actually menu-items and their main menu name is Information
I want to show their main-menu name as well like so:
Information
-FAQ
-Contact Us
I have this code
$menu = JFactory::getApplication()->getMenu();
$parent_title = $menu->getItem(2)->title;
The main menu named Information has id 2 but it does not show anything :(
So how do I get that menu name?
Lets try this way:
$menu = JFactory::getApplication()->getMenu();
$active = $menu->getActive();
$parent = $menu->getItem($active->parent_id);
var_dump(parent);
and tell us what you get : )
if this don't work for you, change line with parent to:
$parent = $menu->getItems('id', $active->parent_id);
regards
Is there a way to get the Alias field of a main menu item in Joomla 1.5 from mod_mainmenu module? I know you can access the menu using this code:
$menu = JSite::getMenu();
I need to use the Alias field to hold a sub-title for the menu item. Is it possible to fetch this from the modMainMenuXMLCallback() function in mod_mainmenu? Thanks.
$menu = JSite::getMenu();
$alias = $menu->getItem($id)->alias; // if you have id of menu
$menu = JSite::getMenu();
$alias = $menu->getActive()->alias; // alias of active menu
This comes from a PHP script in a theme of WordPress. I need to return blog entries with category that has an ID of 20.
<?php /* make a new query for grid items (in single page) */
$new_query_arg = 'paged='.$paged;
// use this code if you want filter items by category.
$arr_catID = array();
foreach( get_the_category() as $cat) $arr_catID[] = $cat->cat_ID;
if ( count($arr_catID) ) $new_query_arg .= '&cat=' . join(',', $arr_catID);
query_posts($new_query_arg);
?>
If you want to only display entries with category ID 20, then replace all the code with:
query_posts('paged='.$paged.'&cat=20');