WordPress Menu highlight with pagination - wordpress

I have a WordPress website with a menu similar to this one: Yahoo news, so basically:
a horizontal menu with two levels, the first with main categories the second with sub-categories (in the "Home" menu item, the subcategory is the page "about us".
I am using the build-in Menu of WordPress
The problem I have is with the pagination and also with the search form:
When I am in the Home Page and I
click any pagination number, the
subcategories of "Home" menu item
disappears, and most importantly,
the "Home" menu item is no longer highlighted.
When I search something, again the
same issue, the "Home" is no
longer highlighted, and there is
no subcategory under "Home" menu
item.

I have found a solution:
1) Open header.php, and add this in the body tag:
<body<?php if ( is_home() || is_search() || is_404() ) { echo ' class="main"';} else { echo '';} ?>>
2) Add a class that target the home class, so:
body.main #main-menu ul li.menu-item-home a {
background: #313B47;
color: white;
}
Now if I am in the home page, or in the search page, or 404 error page, WordPress adds class="main" to the body, so I can easily target the menu item which I want.

Related

drupal 7 active menu item is set for multiple menu links with contextual filter

I have 3 pages in my menu that basically link to the same page, but with a different contextual filter on them.
Is there any way to only set the correct "page" as the active page in my menu so the other links don't get the active menu item style?
These are the links:
xxx.ons-aanbod/% (contextual filter)
xxx.ons-aanbod/diversiteit (VOORGROEPEN)
xxx.ons-aanbod/architectuur (VOOR SCHOLEN)
I'm not sure but think you can hook menu links , like this :
function MODULENAME_menu_link_alter(&$item)
{
$path = current_path(); // get path of current page displayed
if($item['link_path']== $path) // or something like
{
// add class active
}
}
https://api.drupal.org/api/drupal/modules!system!system.api.php/function/hook_menu_link_alter/7.x
https://api.drupal.org/api/drupal/includes%21path.inc/function/current_path/7.x

How to display the menu name of a menu in wordpress

I have created a menu with the name "alggemeen" and added some items to it.
I have assigned the menu to the theme location Footer menu with the slug 'footer-menu'.
Now I know how to display the content of menu and also the menu_location. But I want to display the name the wordpress admin will provide in backend for the menu, on my website.
I would like to echo the name of the menu that has been allocated to the menu with a theme location of 'Footer Menu(slug = 'footer-menu')'.
So as shown above in the picture Currently alggemeen is the menu assigned to Footer Menu so my front end should echo algemeen.
Try this code :
$menu_location = 'header';
$menu_locations = get_nav_menu_locations();
$menu_object = (isset($menu_locations[$menu_location]) ? wp_get_nav_menu_object($menu_locations[$menu_location]) : null);
$menu_name = (isset($menu_object->name) ? $menu_object->name : '');
echo esc_html($menu_name);

Wordpress add class to specific menu item

I have an onepage template. Menu has Home, Services, Blog, Contact etc and working with <div id="blog">. If someone clicks menu item "Blog" goes to domain.com/#blog. If click on blog title goes to domain.com/blog/blog-title. In this page i want an active class in menu item "Blog". This is my code
add_filter('nav_menu_css_class' , 'my_nav_special_class' , 10 , 2);
function my_nav_special_class($classes, $item){
if(( is_page_template( 'page-blog.php' ) ) && ($item->title == 'BLOG')) {
$classes[] = 'active';
}
return $classes;
}
While loading page menu item "Blog" is active. When stops loading menu item "Home" is active.
How can i keep class="active" only in menu item "Blog" ?

Wordpress menu: automatically add categories

In Wordpress, i'd like to automatically add categories from custom post types in a submenu item, with respecting the hierarchy.
This is how my menu will look like:
Projects
Categories
Category 1
Category 2
Subcategory1
Subcategory2
Category 3
....
Other Sub item
ItemX
ItemY
Any idea on how to automatically add all categories in the menu, under "Categories" item?
(It's a menu created in Appearance > Menus)
I guess i might use wp_list_categories() but I have no idea where to place this...
Thanks !
Try this filter
It's a little hacky but i managed to add some stuff into my menus from here
WP nav menu itmes
function wp_nav_menu_items( $items, $args ) {
if ( "primary" == $args->theme_location ) { //check what menu it is
//Do Stuff here
}
return $itmes;
}
add_filter( wp_nav_menu_items, wp_nav_menu_items );

menu highlight current post

I´m using a wordpress default installation with a very basic theme. The problem I have is that when I´m viewing a post, a normal post or a custom post type, the menu does not get highlighted.
For example, I click "Blog" and the blog posts archive page shows and the menu is still highlighted properly, but when I open a random post the menu highlight is gone.
is there any fix for this?
Coincidentally, I set something up for this yesterday for a client's theme. You probably have a class for highlighting the menu item? Can you post what your theme code looks like--probably this is in something like header.php within the theme.
What I did was compare the title of the page with the menu item, and set that class. Something like:
class="<?php if(trim(wp_title("",false))=="Home") echo "active"; ?>"
which sets the class to "active" if the wp_title is "Home". This is a static navigation menu with links for each page; yours might be dynamic within a loop printing the page titles for navigation, so it would be good to see your code to be able to help.
or you can have a condition,
<?php if (is_single() ?>
For highlighting a particular menu,you can try this in ur style.css file of your website:-
#nav li.current_page_item a{
color:#fff !important;
background-color:#82bd42;
text-decoration:none;
-moz-border-radius: 3px;
-webkit-border-radius: 3px;
border-radius: 3px;
padding:10px 10px;
}
Where nav is the id of the <nav id="id"> tag where menu is being located in
header.php,like this:-
<nav id="nav">
<?php wp_nav_menu( array( 'theme_location' => 'primary' ) ); ?>
</nav>

Resources