HighLighted item menu when clicked in codeIgniter - php

how can i make the active menu in codeIgniter, its not when hover, but when clicked, for example if we are in some category, that item in the menu is highlighted, how can be this done in CI?

Depends on your routing and menu generation script. Esiest method is to check for segments in uri. For example for static menu You can do this:
<?php $class = $this->uri->segment(1)=="someController"?"highlighted":""; ?>
Menu item

There are a few steps involved here.
Firstly, you need to determine which is the 'current' menu item or category. If you can structure your site so that there's a direct relationship between your URL structure and your top level menu items (and / or your categories) then this will help a lot.
You'll need a common section of code that generates your main menu. This code could iterate through an array of menu item titles to produce the HTML for the menu. If the array had keys for the URL segment and values for the menu item text...
$menuItems = Array(
"/home" => "Home",
"/products" => "Products",
"/faq" => "FAQ",
"/aboutus" => "About Us"
);
(Leading slashes included for clarity as to which are the URI segments and which are the Menu Titles only - you would usually omit the leading slash)
... then, while you're iterating through, you could check each item against the relevant segment of the current URL.
Secondly, having worked out which is the current item, you could add a css class to the relevant HTML element.
e.g.
$menuHtml = "<ul class='menu'>\r\n";
foreach($menuItems as $segment => $title) {
if($segment == $this->uri->segment(1)) {
$menuHTML .= "<li class='current'>$title</li>";
} else {
$menuHTML .= "<li>$title</li>\r\n";
}
}
$menuHtml .= "</ul>";
You'd then need to apply the required highlight styles in CSS to the li.current element:
li.current {
<your-highlight-css-styles-here>
}

Related

PHP active section after page refresh

I am trying to create a one page navigation menu in PHP. The reason I want it to be in PHP is because I can add other code later and I want the user to be on the same page even after a refresh.
I have an unordered list with generated list items. The list items are generated with an array.
(I want the key/values separated because the actual code will be different. This is just an example.)
$nav = array(
"home" => "home",
"about" => "about",
"contact" => "contact"
);
echo '<ul>';
foreach( $nav as $id => $name )
{
echo '<li>'.$name.'</li>';
}
echo '</ul>';
// outputs:
<ul>
<li>home</li>
<li>about</li>
<li>contact</li>
</ul>
Then I want some divs to be the sections
foreach( $nav as $s_id => $s_name)
{
echo '<div id="'.$s_id.'">Some section text</div>';
}
Now I need to know how can I set the display to either block or none based on current id active?
While I personally think this is more of a job for JavaScript, in the question and a comment above you specify that you want to do this in PHP. In that case those anchor tags will need to be actual links to the page. For the sake of this example I'll assume the page is called index.php. (If it's something different, just use whatever your page is.)
Update the anchors to be links to the page with query string parameters:
<li>home</li>
<li>about</li>
<li>contact</li>
Then in your PHP code you'd determine which one is "active" by the active query string parameter. Start with a default of "home" in case no parameter is specified (it's the user's first time on the page):
$active = "home";
if (isset($_GET["active"])) {
$active = $_GET["active"];
}
Then when rendering the <div> elements you can specify their style:
foreach( $nav as $s_id => $s_name)
{
echo '<div id="'.$s_id.'" style="display:'.($s_id == $active ? "block" : "none").'">Some section text</div>';
}

How to display sub categories using Collapsing Categories plugin

My site is using the Collapsing Categories plugin (Robert Felty). As a sidebar widget, it is working the way we want it. But now I've written a page, and I'm inserting the php code in the page, and I don't know how to make it display the same Blog sub categories that it displays on any blog post page.
The sub-categories of the blog category are:
- from-the-experts
- grilling-lifestyle
- grilling-tips-and-techniques
But when I add the php code to the page (using the page editor), it outputs all the top level categories, instead:
►Blog (118)
►Lamb (1)
►News (1)
►Recipes (59)
►Uncategorized (4)
This is the php code, taken from the readme.txt page:
[php]
echo "<ul class='collapsCatList'>\n";
if (function_exists('collapsCat')) {
collapsCat();
} else {
wp_get_categories('your_options_here');
}
echo "</ul>\n";
[/php]
What I want to know is what I have to put in that code to display only the few sub categories I want displayed.
Answered by plugin author here
$my_options=array(
'inExclude' => 'include',
'inExcludeCats' => 'blog',
'showTopLevel' => false
);
echo "<ul class='collapsCatList'>\n";
if (function_exists('collapsCat')) {
collapsCat($my_options);
} else {
wp_get_categories('your_options_here');
}
echo "</ul>\n";

Wordpress Sub Menu / Show Sister Pages

I am currently using this snippet to show children of the current page.
<?php wp_list_pages('title_li=&child_of='.$post->ID.''); ?>
when I click one of those children pages the sub nav disappears because there are no child pages of the current page, so what I need it to do is continue to show the sister pages of the current page.
What is the best way to do this?
You could request the parent ID instead of the post ID if there is no childs on the current page :
$page_query = new WP_Query();
$all_pages = $page_query->query(array('post_type' => 'page'));
$childs = get_page_children($post-ID, $all_pages);
if(!empty($childs)) {
// display the page childs
wp_list_pages($post->ID);
} else {
// display the page siblings
wp_list_pages(wp_get_post_parent_id($post->ID));
}
get_page_children is used to check if there is any child for the current page - the WP_Query part is needed for get_page_children which required an array of all the pages to look in.

Adding dynamic Link to menu Item in wordpress

I want to have 'Text only' in menu of my wordpress website without plugin.So how to set dynamic link to menu item in wordpress?
try this one
add_filter('wp_nav_menu_items', 'menu_link', 10, 2);
function menu_link($items, $args) {
if ($args->menu == 'footer'){
$items .= '<li class="copyright ">Text Only</li>';
$items .= '<li class="copyright ">Print Page</li>';
}
return $items;
// return $items1;
}
You will have to recreate the complete menu walker to make it text only, or remove the anchor through jquery. But if you just want a menu item that doesn't link anywhere, create a normal link menu item, and instead of an actual link use #.

Drupal template, verify that the page is a menu item

Helló!
I want to examine that the actual page is in the menu.
I want to do with drupal template (page.tpl.php).
For example:
if ($page_is_a_menuitem):
echo "This page is in the menu";
else:
echo "This page not in the menu";
endif;
Because if the page is in the menu a want to highlight the title.
Sorry for my bad english.
Try this
this is return to a all menu list :- menu_get_menus(true)
like this
Array
(
[menu-footer-menu] => Footer Menu
[main-menu] => Main menu
[management] => Management
[navigation] => Navigation
[user-menu] => User menu
)
and you want to get main menu inside a all list to use this
$tree = menu_tree_all_data('main-menu')
this is return in main-menu in all items
otherwise you use a this code
$path = current_path();
$selected_menu= '';
$menu = menu_link_get_preferred($path = NULL, $selected_menu = NULL);
$menu is return a current page manu detail after you want
echo "this menu is ".$menu['menu_name'] .' and menu title is '.$menu['title'];
get the whole menu tree
$tree = menu_tree_page_data('primary-links');
and check in page tpl if the node id exists in the menu...

Categories