Wordpress add class to specific menu item - wordpress

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" ?

Related

WordPress admin menu: custom logout link shows as submenu item instead of menu item

I added a custom logout link to my WP admin menu, but instead of appearing as a top-level menu item, it is displayed as a submenu item (smaller font size, left padding). The link itself works perfectly though. Any ideas how the code can be changed? Thanks!
current admin menu
The code I use is from this thread.
add_action('admin_init', 'text_domain_logout_link');
function text_domain_logout_link() {
global $menu;
$menu[9999] = array(__('Logout'), 'manage_options', wp_logout_url());
}
Can you try this?:
add_action('admin_menu', 'text_domain_logout_link');
function text_domain_logout_link() {
global $menu;
$menu[9999] = array(__('Logout'), 'manage_options', wp_logout_url());
}
Tested and works on my wordpress
Updated:
If you want to show it to top-level then use this code:
add_action('admin_menu', 'text_domain_logout_link');
function text_domain_logout_link() {
global $menu;
$menu[9999] = array(__('Logout'), 'manage_options', wp_logout_url());
// add class
$menu[9999][4] = "menu-top toplevel_page_menu";
// Add Icon
$menu[9999][6] = "dashicons-update";
}
So it will look like this:

WordPress add Login/Logout to menu editor

Ok, so I found this code, which I modified to suit my needs. Btw, I'm using WooCommerce, which explains the "wc" in some of the function calls:
//Add login/logout link to primary menu
add_filter( 'wp_nav_menu_items', 'add_loginout_link', 10, 2 );
function add_loginout_link( $items, $args ) {
if (is_user_logged_in() && $args->theme_location == 'primary') {
$items .= '<li>Log Out</li>';
}
elseif (!is_user_logged_in() && $args->theme_location == 'primary') {
$items .= '<li>Log In</li>';
}
return $items;
This adds the login/logout menu items, and they work fine. However, they're stuck at the end of the menu, at the moment. I'd like to be able to edit the position using the editor in wp-admin. The solution I thought of was to maybe just create login and logout pages, and use header location redirects with those lines of code in them to get to the proper URLs, but the issue I see with that is that there will always be a login item and logout, no matter what status the user is currently in. Would there maybe be a way to dynamically add a site-wide CSS rule to hide the opposite menu item, based on the log in status?
Or is there an easier way?
Not the best idea but you can try.
Create in wp-admin menu section new menu item like "Custom Link"
Log Out
http://www.example.com/account/customer-logout/
Log In
http://www.example.com/account/
And add a custom class to a WordPress menu item to manage visibility
For example, you will see "logged-in" class on the body of the page and hide "Log In" link or change it to "Account" with the same link.

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 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 );

WordPress Menu highlight with pagination

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.

Resources