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 #.
Related
I would like to add a logo ( get_custom_logo() ) in the Genesis navigation bar but when it's blank, there's to be text. I've achieved this in a similar fashion for the header but cannot for the primary nav. The end result DOES work but when the user goes to the "customize" part of "site identity" the change is not visible, until you hit "publish" then refresh the actual page.
I need the change to be seen in "customize" mode as the logo is being changed. Here's the code the semi-functions...
add_filter( 'wp_nav_menu_items', 'prefix_add_menu_item', 10, 2 );
/**
* Add Menu Item to end of menu
*/
function prefix_add_menu_item ( $items, $args ) {
if( $args->theme_location == 'primary' ) {
if (( has_custom_logo())) {
$thelogo = get_custom_logo();
$items .= '<li class="menu-item">' . $thelogo . '</li>';
} else {
$items .= '<li class="menu-item">Theme Name</li>';
return $items;
}
return $items;
}
}
Ideally, I'd like to apply this method throughout the site for other hooks. If anyone can point to what I'm doing wrong, that would help.
Cheers.
It won't refresh in preview unless you add a custom setting to $wp_customize for your image or you add a new PHP class for the site logo. Either way, it's not an easy fix.
I want to display a search bar in the Wordpress navigation menu, for this I added this in my functions.php file:
add_filter('wp_nav_menu_items', 'sydney_child_add_serch_from_to_nav', 10, 2);
function sydney_child_add_serch_from_to_nav($items, $args){
if ($args->theme_location == 'primary') {
$items .= '<li class="top-search-menu">'.get_search_form(false).'</li>';
}
return $items;
}
The problem being that my sites must be valid WCAG 2.0 (Level A), but I have the following error:
I tried several solutions to add a label but without success. I have the same error when using a search plugins.
Thank you in advance.
I have just noticed that my menu items id are not getting generated by wordpress.
I expect it to be something like this
<li id='menu-item-2091' class='menu-item menu-item-209'>
<a href='www.mysite.com/members/<?php echo $user_info->user_login; ?>'>Profile</a></li>
but i get this without the menu item id.
<li class='menu-item menu-item-209'>
<a href='www.mysite.com/members/<?php echo $user_info->user_login; ?>'>Profile</a></li>
This error is occurring in my main menu, but my top menu seems to generate the menu item id properly.
I have looked around the menus area and can't find any clues why??
Here is a filter that hacks it out
add_filter ('wp_nav_menu_items','gfb_missing_id_fix', 10, 2);
function gfb_missing_id_fix($menu, $args) {
if($args->theme_location == "primary-menu"){
$dom = new DOMDocument;
$dom->loadHTML($menu);
foreach($dom->getElementsByTagName('li') as $element ) {
$classes = $element->getAttribute("class");
preg_match("/menu-item-\d+/", $classes, $output_array);
$element->setAttribute("id", $output_array[0]);
}
$menu = $dom->saveHTML();
}
return $menu;
}
General info
I use my navigation twice (mobile and desktop with different menu ids) and only the second menu is missing the item ids. This makes sense, because it secures that menu-item-ids are only used once.
JQuery Solution
For me the following javascript / jquery solution does the trick. It adds menu-id-ID (with ID as the actual menu-item-id) to each menu item.
var $mainMenu = $("#main-nav"); // your menu_id used in wp_nav_menu
$mainMenu.find("li").each(function(index, element) {
var $element = $(element);
var classes = $element.attr("class"),
ID = classes.match(/menu-item-(\d+)/)[1];
$element.attr("id", "menu-id-" + ID); // here 'menu-id-' is prepended to the actual ID
});
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;
}
So after calling wp_nav_menu like:
<?php wp_nav_menu( array('menu' => 'Primary Menu' )); ?>
I end up with:
<li id="menu-item-1">
<li id="menu-item-2">
... ids like menu-item-1, menu-item-2, etc.
Is there some way to replace the numbers with the title of the page instead,so
menu-item-1 becomes menu-item-contact,
menu-item-2 becomes menu-item-store, etc?
You'll need to filter the ID using WordPress's nav_menu_item_id filter, which is applied in the nav-menu-template.php file. It takes three arguments, but the second one is what we need to actually get the title of the menu item, and turn it into something useful for the ID attribute. Here's a function to do that, which works for me in WordPress 3.4.1:
function custom_nav_menu_item_id($id, $item) {
$title = sanitize_title_with_dashes($item->title);
return 'menu-item-'.$title;
}
add_filter('nav_menu_item_id', 'custom_nav_menu_item_id', 10, 2);
This uses another WordPress function to sanitize the title into a string usable for the ID attribute (or any other HTML attribute).
Note that this function get's the menu item's title, which migth be different than the post or page title if you change it in the Menu panel from the admin.
Based on #Jeremy answer : I just add sanitize_title() to remove accents and special char.
function custom_nav_menu_item_id($id, $item) {
$title = sanitize_title_with_dashes(sanitize_title($item->title));
return 'menu-item-'.$title;
}
add_filter('nav_menu_item_id', 'custom_nav_menu_item_id', 10, 2);
/** for a better wp cleaning take a look # this gist and include it in your function.php */