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.
Related
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")?
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' => ''
);
I do have two menus in my Wordpress-Page which are called
Mainmenu(Primary Menu)
Footer
I associated the "Mainmenu" to the header of the website. I want the footer of the Wordpresspage to look exactly like the header, only difference should be the Links in it. It should not be taken from the "Mainmenu", but from the menu called "Footer". The function of the Header looks like this:
echo '<nav id="rtp-primary-menu" role="navigation" class="rtp-nav-wrapper' . apply_filters( 'rtp_mobile_nav_support', ' rtp-mobile-nav' ) . '">';
rtp_hook_begin_primary_menu();
/* Call wp_nav_menu() for Wordpress Navigaton with fallback wp_list_pages() if menu not set in admin panel */
if ( function_exists( 'wp_nav_menu' ) && has_nav_menu( 'primary' ) ) {
wp_nav_menu( array( 'container' => '', 'menu_class' => 'menu rtp-nav-container clearfix', 'menu_id' => 'rtp-nav-menu', 'theme_location' => 'primary', 'depth' => apply_filters( 'rtp_nav_menu_depth', 4 ) ) );
} else {
echo '<ul id="rtp-nav-menu" class="menu rtp-nav-container clearfix">';
wp_list_pages( array( 'title_li' => '', 'sort_column' => 'menu_order', 'number' => '5', 'depth' => apply_filters( 'rtp_nav_menu_depth', 4 ) ) );
echo '</ul>';
}
rtp_hook_end_primary_menu();
echo '</nav>';
The Theme i am using is rtPanel. Can anyone tell me how i simply can change this function in the way that it will load its content from the Menu called "Footer" and not from the Primary Menu which is "Mainmenu"?
Please dont worry about the CSS and everything, i will take care about this. I really just want to now how i have to change this function to load the "Footer"-Menu in it and not the Primary-Menu(Mainmenu).
As an Attachment how it look in the menu:
Thank you very much advance!
This is really simple, in fact when you start developing wordpress theme this might be one of the very first items you need to learn.
You need to register a menu location for footer seciton. You can learn more in codex
register_nav_menu( 'footer', __( 'Footer Menu', 'theme-slug' ) );
This will register the menu for you. Since you already have the menu location just find the location parameter. We're going to use that in the following code. For now assume it 'footer' like the above one. And the code for the footer menu according to your referenced code-
echo '<nav id="rtp-primary-menu" role="navigation" class="rtp-nav-wrapper' . apply_filters( 'rtp_mobile_nav_support', ' rtp-mobile-nav' ) . '">';
//rtp_hook_begin_primary_menu(); // we better disable this hook as it intends for the header primary menu
/* Call wp_nav_menu() for Wordpress Navigaton with fallback wp_list_pages() if menu not set in admin panel */
if ( function_exists( 'wp_nav_menu' ) ) {
wp_nav_menu( array( 'container' => '', 'menu_class' => 'menu rtp-nav-container clearfix', 'menu_id' => 'rtp-nav-menu', 'theme_location' => 'footer', 'depth' => apply_filters( 'rtp_nav_menu_depth', 4 ) ) );
} else {
echo '<ul id="rtp-nav-menu" class="menu rtp-nav-container clearfix">';
wp_list_pages( array( 'title_li' => '', 'sort_column' => 'menu_order', 'number' => '5', 'depth' => apply_filters( 'rtp_nav_menu_depth', 4 ) ) );
echo '</ul>';
}
//rtp_hook_end_primary_menu(); // we better disable this hook as it intends for the header primary menu
echo '</nav>';
Notice the theme_location parameter in wp_nav_menu() function? This is the one which plays behind to get you menu correctly.
Now play around with CSS and you might get what you intend.
Hope this might help you.
I am very new to PHP. I am handling a template which has been built and I am getting really confused. All I want to do is change the navigational bar names. Now normally with what I do it would be HTML but this seems to be defined in PHP and I am lost where to look.
My code is below I have targeted it down to where it is held as I have seen class in Google debugger. Now how can I find the list which is defined somewhere in my code. Which function do I go and search for next?
<nav class="site-navigation<?php echo esc_attr($menu_description); ?>">
<?php
$locations = get_theme_mod('nav_menu_locations');
/* Check if menu is selected */
$walker = '';
$menu = '';
$locations = get_theme_mod('nav_menu_locations');
if($locations && $locations['primary']) {
$menu = $locations['primary'];
if( (isset($_GET['page']) && $_GET['page'] == 'one-page') ) {
$menu = 21;
}
$walker = new description_walker();
}
wp_nav_menu( array(
'container' => false,
'menu_class' => '',
'echo' => true,
'before' => '',
'after' => '',
'link_before' => '',
'link_after' => '',
'depth' => 0,
'walker' => $walker,
'menu'=>$menu
));
?>
<button class="fa fa-search desktop"></button>
</nav>
<?php
}
It looks like your issue is not in the code, as the above snippet is simply defining the menus for use through the WP admin.
Login to the back-end and visit Appearance > Menus and you should see a menu here, or the ability to create one.
Good Luck!
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");