How to Remove 'Home' Link from wp_nav_menu! - php

How do I get rid of the 'Home' link from appearing at the top of my links when using <?php wp_nav_menu( array('menu' => 'news', 'show_home' => false)); ?>
I tried 'show_home' => false and 'show_home=0' but neither worked.

This should be in your functions.php
function page_menu_args( $args ) {
$args['show_home'] = FALSE;
return $args;
}
add_filter( 'wp_page_menu_args', 'page_menu_args' );
EDIT: Dont forget to add this to wherever your menu is supposed to print out:
wp_nav_menu( array('echo'=>true));

The following worked for me:
_nav_menu( array( 'container_id' => 'topmenu', 'depth' => 0, 'menu_class' => 'sf-menu', 'theme_location' => 'topmenu' ) );
And I add
function page_menu_args( $args ) {
$args['show_home'] = FALSE;
return $args;
}
add_filter( 'wp_page_menu_args', 'page_menu_args' );
In the functions.php file.

If you are like me looking to remove the 'home' link from the default wordpress menu (wp_page_menu) and the home is a page (not blogposts), this is one way to solve it:
in functions.php:
function getPageBySlugname($slugname) {
$args = array(
'post_type' => 'page',
'hierarchical' => 0,
'post_status' => 'publish'
);
$pages = get_pages($args);
foreach ($pages as $page) {
if ($page->post_name == $slugname) {
return $page->ID;
}
}
}
in header.php
wp_page_menu(array(
'container' => 'div',
'show_home' => false, // Not sure what this is hiding, maybe if you have blogposts as home??
'echo' => true,
'exclude' => getPageBySlugname('homepage-slugname'), // change this to your slugname
));

You're making it way too hard! Instead, use CSS display: none for that particular .home item of a custom menu. It works like a charm. Example:
menu-blogroll .home {display:none !important;}

I used jquery to fix the same.
$("div.menu > ul li:first-child").css("display","none");

Related

WordPress wp_nav_menu is not working in header and footer file in WordPress 5.6 version

I have tried to display menu in header and footer file but I got below error.
Warning: implode(): Invalid arguments passed in C:\xampp\htdocs\XXX\wp-includes\class-walker-nav-menu.php on line 153
I used below code in footer and header file.
wp_nav_menu(
array(
'theme_location' => 'footer',
'menu_class' => 'menu-wrapper',
'container_class' => 'primary-menu-container',
'items_wrap' => '',
'fallback_cb' => false,
)
);
I have also tried to simple way like
wp_nav_menu( array( 'theme_location' => 'header' ) );
But it's always showing waning in WordPress version 5.6.
Someone please help me how can i resolve this error.
if you use something like that:
function change_menu_item_css_classes( $classes, $item, $args, $depth ) {
if($args->menu === 'Main'){
$classes = ['your_class'];
return $classes;
}
}
don't forget add else
function change_menu_item_css_classes( $classes, $item, $args, $depth ) {
if($args->menu === 'Main'){
$classes = ['your_class'];
return $classes;
} else {return [''];}
}
You have this piece of code in there:
'theme_location' => 'footer'
Did you actually assign your navigation menu to a menu location called "footer" (or "header" for your second example) in the WP backend (under "Settings > Menus > Menu Positions")?

Is it possible to pass a nav widget in the Wordpress (Sidebar) new $args?

I've spent some time searching, but I haven't really found anything concrete in regards to passing new $args to a navigation widget. I did stumble across this post. However, I think the answer is a little overkill for what I'm trying to achieve.
To sum up the linked post it basically goes on to show how you could accomplish what I need, but only if an entirely new widget is created.
Specifically, I'm looking to either merge or overwrite the following
$args exclusively for a menu widget placed within a Wordpress sidebar;
wp_nav_menu( array $args = array(
'menu' => "header-quicklinks",
'menu_id' => "quicklinks",
'theme_location' => "sidebar-header"
) );
If possible I would like to pass the ID of the widget, in my case nav_menu-6; to the function and have the $args only apply to that menu specifically, this way I can touch up the code to target other menus should I have the requirement.
Currently tinkering with the following;
function widget_nav_args($args){
$menu = $args['menu'];
if($menu->term_id === "menu-quick-links") { // < Error: non-object.
return array_merge( $args, array(
'menu_class' => 'TESTING', // for testing.
// More settings here ...
) );
}
return $args;
}
add_filter('widget_nav_menu_args', 'widget_nav_args');
You are nearly there. The widget_nav_menu_args filter accepts more parameters than just the $args for the nav. You want to look at the widget arguments which is the 3rd paremeter. It would look something like this:
function widget_nav_args( $nav_menu_args, $nav_menu, $args, $instance){ // <- notice extra params..
if( $args['id'] === 'sidebarheader' ) { // < This is where we check if it's the right widget
return array_merge( $nav_menu_args, array(
'menu_class' => 'TESTING', // for testing.
// More settings here ...
) );
}
return $nav_menu_args;
}
add_filter('widget_nav_menu_args', 'widget_nav_args', 10, 4);
Notice I had to explicitly say how many arguments to pass to my filter function. Be sure to read through the documentation in the WP Codex here.
Hope that helps!
add_filter('widget_nav_menu_args', 'my_wp_nav_menu_args');
function my_wp_nav_menu_args($args) {
if (is_page(2) //Only target page 2
&& $args['theme_location'] === 'primary') { // Check and only target the primary menu
$args['menu'] = 'Menu for Profile';
}
return $args;
}
sample arguments are as below.
$arguments = array(
'menu' => '',
'container' => 'div',
'container_class' => '',
'container_id' => '',
'menu_class' => 'menu',
'menu_id' => '',
'echo' => true,
'fallback_cb' => 'wp_page_menu',
'before' => '',
'after' => '',
'link_before' => '',
'link_after' => '',
'items_wrap' => '<ul id="%1$s" class="%2$s">%3$s</ul>',
'item_spacing' => 'preserve',
'depth' => 0,
'walker' => '',
'theme_location' => ''
);

Modal on wp menu link

I'm trying to learn some new stuff but I stuck to something easy.
I managed to manipulate a link from wp menu using this code
add_filter( 'nav_menu_link_attributes', 'menu_item_data_toggle', 10, 2 );
function menu_item_data_toggle( $atts, $item) {
// Manipulate attributes
if ( 74 == $item -> ID )
$atts['data-toggle'] = 'modal';
return $atts;
}
but I need to use this modal with "if" attribute for login user and I can't figure how to.
The code for this modal I want to use is from a button
<?php
if( is_user_logged_in() ){
$account_manage = ''.classifieds_get_option( 'my_profile_looks' ).'';
$submit_ad = add_query_arg( array( $classifieds_slugs['subpage'] => 'submit_ad' ), classifieds_get_permalink_by_tpl('page-tpl_my_profile') );
$modal = '';
}
else{
$account_manage = ''.classifieds_get_option( 'login_looks' ).'';
$submit_ad = '#register';
$modal = 'data-toggle="modal"';
}
$locations = get_nav_menu_locations();
if ( isset( $locations[ 'top-navigation' ] ) ) {
wp_nav_menu( array(
'theme_location' => 'top-navigation',
'menu_class' => 'nav navbar-nav clearfix',
'container' => false,
'echo' => true,
'items_wrap' => '<ul class="%2$s">%3$s',
'depth' => 10,
'walker' => new classifieds_walker,
) );
}
if(get_option('users_can_register')){
echo '<li>'.$account_manage.'</li><li class="submit-add">'.esc_html__( 'SUBMIT AD', 'classifieds' ).'</li></ul>';
}
?>
Can you please me give me a hint to figure it out and use that menu link with this modal attributes?
Thank you!

wp_menu in wordpress php function

I'm trying to create a simple nav in wordpress, however the wp_nav_menu parameters are ignored , am I missing something obvious?
I'm using a blank template called html5 blank. The Steps i've taken so are listed below
1) Register the menu in functions.php
2) From word press back end Create a menu called 'p' and assign pages
3) Give the menu a theme location
Registering and assigning menu location work fine , some but for some reason the wp_ parameters are ignored EG container , menu class, menu id etc...
If i inspect element, the container 'nav' is missing and the li items have default word press classes
BELOW is my code
Code in header.php
<?php html5blank_nav() ?>
Code in functions.php
function html5blank_nav()
{
wp_nav_menu(
array(
'theme_location' => 'primary pete',
'menu' => 'p'
'container' => 'nav',
'container_class' => '',
'container_id' => '',
'menu_class' => 'slimmenu',
'menu_id' => 'navigation',
'echo' => true,
'fallback_cb' => 'false',
'before' => '',
'after' => '',
'link_before' => '',
'link_after' => '',
'items_wrap' => '<ul>%3$s</ul>',
'depth' => 0,
'walker' => ''
)
);
}
// Register HTML5 Blank Navigation
function register_html5_menu()
{
register_nav_menus(array( // Using array to specify more menus if needed
'primary' => __('primary pete', 'Primary Menu'),
'sidebar-menu' => __('Sidebar Menu', 'html5blank'),
'extra-menu' => __('Extra Menu', 'html5blank')
));
}
// Remove the <div> surrounding the dynamic navigation to cleanup markup
function my_wp_nav_menu_args($args = '')
{
$args['container'] = false;
return $args;
}
// Remove Injected classes, ID's and Page ID's from Navigation <li> items
function my_css_attributes_filter($var)
{
return is_array($var) ? array() : '';
}
// Remove invalid rel attribute values in the categorylist
function remove_category_rel_from_category_list($thelist)
{
return str_replace('rel="category tag"', 'rel="tag"', $thelist);
}
// Add page slug to body class, love this - Credit: Starkers Wordpress Theme
function add_slug_to_body_class($classes)
{
global $post;
if (is_home()) {
$key = array_search('blog', $classes);
if ($key > -1) {
unset($classes[$key]);
}
} elseif (is_page()) {
$classes[] = sanitize_html_class($post->post_name);
} elseif (is_singular()) {
$classes[] = sanitize_html_class($post->post_name);
}
return $classes;
}
Many thanks,
P
When i try to reproduce this the menu with container_class => 'slimmenu' is shown.
Are you sure when you add a class to the container_class parameter this is not shown in the inspector?
Also noticed a missing comma after the parameter 'p' when i copied your code. But i don't think it will resolve your problem.

wp_nav_menu exclude pages from menu

I am trying to exclude pages from wp_nav_menu
Here is the function to exclude pages from menu
Works well when
<?php $page = get_page_by_title('my title' );
wp_nav_menu( array( 'theme_location' => 'menu-2', 'exclude' => $page->ID ) ); ?>
Works properly
BUt when using
<?php $page = get_page_by_title('my title' );
$pag1 = get_page_by_title('my title 1' );
$pag2 = get_page_by_title('my title2' );
wp_nav_menu( array( 'theme_location' => 'menu-2', 'exclude' => $page->ID,$pag1->ID,$pag2->ID ) ); ?>
Doesn't work properly.
try this
wp_nav_menu( array( 'theme_location' => 'menu-2', 'exclude' => '".$page->ID.",".$pag1->ID.",".$pag2->ID."' ) );
checkout this tutorial
Here is the correct solution
I hope it helps some other people
<?php
$page = get_page_by_title('my title' );
$pag1 = get_page_by_title('my title 1' );
$pag2 = get_page_by_title('my title2' );
$ids = "{$page->ID},{$pag1->ID},{$pag2->ID}";
wp_nav_menu( array( 'theme_location' => 'menu-2', 'exclude' => $ids ) );
?>
Managed it using the exclude method, but exclude by page id:
wp_nav_menu(
array(
'theme_location' => 'menu-2',
'exclude' => '599, 601'
)
);
(599 & 601 are page id's)
which is written here:
http://bacsoftwareconsulting.com/blog/index.php/wordpress-cat/wordpress-tip-how-to-exclude-pages-from-wp_nav_menu-function/
and finding the page id manually is written here:
http://bacsoftwareconsulting.com/blog/index.php/wordpress-cat/how-to-find-tag-page-post-link-category-id-in-wordpress/
It should work if you put the pages that you want to exclude into an array.
Check out this: http://ehsanis.me/2010/11/30/wordpress-wp_nav_menu-to-exclude-pages/
You can use a Custom Walker Function to just skip the rendering of the menu item.
wp_nav_menu( array(
'theme_location' => 'menu-2',
'walker' => new custom_navigation
) );
class custom_navigation extends Walker_Nav_Menu {
function start_el( &$output, $item, $depth = 0, $args = array(), $id = 0 ) {
parent::start_el($item_html, $item, $depth, $args);
$exclude = array();
$exclude[] = get_page_by_title( 'my title' );
$exclude[] = get_page_by_title( 'my title 1' );
$exclude[] = get_page_by_title( 'my title2' );
if ( ! in_array( $item->object_id, $exclude ) ) {
// add your menu item html to the $output variable
}
}
}

Categories