I want to show an arrow (add text) ">>" behind all the navigation items that have subitems/subpages. Is there a solution with add_filter or do i have to build the menu by myself?
Thanks for help!
Not exactly what you ask..
This adds a class="dropdown" to your menu's
// add a class 'dropdown' to menu items which have a sub menu
add_filter('nav_menu_css_class', 'check_for_submenu', 10, 2);
function check_for_submenu($classes, $item)
{
global $wpdb;
$has_children = $wpdb->get_var("SELECT COUNT(meta_id) FROM wp_postmeta WHERE meta_key='_menu_item_menu_item_parent' AND meta_value='" . $item->ID . "'");
if ($has_children > 0)
array_push($classes, 'dropdown'); // add the class dropdown to the current list
return $classes;
}
Related
In Wordpress, I have set up a file named 'single-event.php' which I am using to render the full data for each post from a Custom Post Type called "Events".
I also have an 'events' page, which pulls some teaser info and a permalink for each post.
However, as the single event pages (using the single-event.php template) are not children of the events page they do not get a .current-page-ancestor class added to the events menu item.
I have seen a suggestion to use the body class and nav item ID to highlight the 'events' menu item, however, the client may reorder or rename pages, and this approach may then break.
Any better suggestions greatly appreciated.
Thanks,
Steve
I reccomend trying nav_menu_css_class filter to add the "active" classes current-menu-item and current_page_item to the menu item based on the current loaded template.
apply_filters( 'nav_menu_css_class', string[] $classes, WP_Post $item, stdClass $args, int $depth )
something like this
add_filter('nav_menu_css_class', 'my_menu_class_filter', 10, 2);
function my_menu_class_filter($classes, $item){
global $post;
if($item->ID !== 123){return $classes;} //Replace the ID with the id of your events teaser page
if($post->post_type === 'event'){
$classes[] = 'current-menu-item';
$classes[] = 'current_page_item';
}
return $classes;
}
edit: Variation for the child page
add_filter('nav_menu_css_class', 'my_menu_class_filter', 10, 2);
function my_menu_class_filter($classes, $item){
global $post;
if($item->ID !== 123){return $classes;} //Replace the ID with the id of your events teaser page
if($post->post_type === 'event'){
$classes[] = 'current-page-ancestor';
$classes[] = 'current-menu-ancestor';
$classes[] = 'current-menu-parent';
$classes[] = 'current-page-parent';
$classes[] = 'current_page_parent';
$classes[] = 'current_page_ancestor';
}
return $classes;
}
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 #.
Hello I want to make a menu in wordpress. When menu item has class 'category' then add to this item submenu. How I can do it? I tried like this, but it only add class.
add_filter('nav_menu_css_class' , 'my_special_nav_class' , 10 , 2);
function my_special_nav_class( $classes, $item )
{
if($item->title == 'kategorie' )
{
$classes[] ='special-class';
}
var_dump($item->title);
return $classes;
}
You need wordpress Menu Walker for this. See Example from here.
Demo Tutorial
I have been struggeling with wordpres menu for a few days now.
I cant find any good information on the internet that i can understand.
I use the menu that wordpress have. And i want to add new items to it text and also some links.
I cant use wordpress admin GUI for this as the links and text comes from MYSQL calls.
So far i can get it to display in my menu but my problem is its alwas ends upp in the end of the menu.
Is there a way to make the menu sorted?
From what i understand the function adds the item to the menu and thats why it ends up at the end.
So i have to break down the hole menu and then make the menu but question is how.
Here is my code this adds a "Login" Or "Logout" link in my menu at the end. How do i move it so it will be first in the menu order?
//Add login/logout link to naviagation menu
function add_login_out_item_to_menu( $items, $args ){
//change theme location with your them location name
if( is_admin() || $args->theme_location != 'main-menu' )
return $items;
$redirect = ( is_home() ) ? false : get_permalink();
if( is_user_logged_in( ) )
$link = '' . __( 'Logout' ) . '';
else $link = '' . __( 'Login' ) . '';
return $items.= '<li id="1log-in-out-link" class="menu-item menu-type-link">'. $link . '</li>';
}
add_filter( 'wp_nav_menu_items', 'add_login_out_item_to_menu', 52, 2 );
If you just want to add the item at first then use
return $items = '<li id="1log-in-out-link" class="menu-item menu-type-link">'. $link . '</li>' . $items;
Just concatenate the $items at the end wit new menu item, this should bring the new item at first.
I want to add a submenu of a wordpress menu into my theme. I want to use the wp_nav_menu function of Wordpress 3.0. And in other words, I want to see the submenu not the subpages which means that wp_list_pages is not the right function because I want the submenu and not the subpages.
Let's assume the menu structure looks like that:
Home
Entry1
Entry3
Entry4
Entry2
Entry5
Entry6
I want that if someone clicks on Entry1 (and makes it the parent) the Theme just shows the submenu of this entry. In the case of Entry1 it's:
Entry3
Entry4
I know that there is a code like that:
<?php
$children = ($post->post_parent) ? wp_list_pages('title_li=&child_of='.$post->post_parent.'&echo=0') : wp_list_pages('title_li=&child_of='.$post->ID.'&echo=0');
if($children) { echo('<ul>'.$children.'</ul>'); }
?>
However, the point is that I'm talking about the menu structure and not the page structure. Oh, and the depth parameter does not work because it means to here and not from here.
I think there could be a solution with a custom walker but I don't know how to implement that.
Function reference for wp_nav_menu
http://codex.wordpress.org/Template_Tags/wp_nav_menu
I'm looking for a solution for this problem for so long so please help me. Thanks a lot.
In order to get this to work I had to hide the .sub-menu as soon as the page loaded. Then, show only the relevant sub-menu by targeting ".current_page_item .sub-menu"
$(document).ready(function() {
$(".sub-menu").hide(); // hide the submenu on page load
$(".current_page_item .sub-menu").show();
)};
This should help: From http://www.svennerberg.com/2009/02/creating-a-submenu-in-wordpress/
<?php
$has_subpages = false;
// Check to see if the current page has any subpages
$children = wp_list_pages('&child_of='.$post->ID.'&echo=0');
if($children) {
$has_subpages = true;
}
// Reseting $children
$children = "";
// Fetching the right thing depending on if we're on a subpage or on a parent page (that has subpages)
if(is_page() && $post->post_parent) {
// This is a subpage
$children = wp_list_pages("title_li=&include=".$post->post_parent ."&echo=0");
$children .= wp_list_pages("title_li=&child_of=".$post->post_parent ."&echo=0");
} else if($has_subpages) {
// This is a parent page that have subpages
$children = wp_list_pages("title_li=&include=".$post->ID ."&echo=0");
$children .= wp_list_pages("title_li=&child_of=".$post->ID ."&echo=0");
}
?>
<?php // Check to see if we have anything to output ?>
<?php if ($children) { ?>
<ul class="submenu">
<?php echo $children; ?>
</ul>
<?php } ?>
One solution is to put another wp_nav_menu function on page and to modify css to hide inactive menu items.