28
votes
Accepted
How to modify navigation menu of the "My Account" page in WooCommerce
For that, you do not need to modify the woocommerce/templates/myaccount/navigation.php.
The best way to customize the "My Account" navigation menu items is to use:
woocommerce_account_menu_items ...
27
votes
Accepted
Add custom classes to anchor in wp_nav_menu
You can do this with the nav_menu_link_attributes filter.
add_filter( 'nav_menu_link_attributes', 'wpse156165_menu_add_class', 10, 3 );
function wpse156165_menu_add_class( $atts, $item, $args ) {
...
14
votes
How to Add <span> to Each Menu Link with link text to data-attr?
Since version 4.4.0 the 'nav_menu_item_args' filter was added. This allows you to set 'link_before' and 'link_after' attributes for each item.
add_filter('nav_menu_item_args', function ($args, $item, ...
8
votes
Displaying Logged-In User Name in Wordpress Menu
The code:
function give_profile_name($atts){
...
}
gives a warning. Better is:
function give_profile_name(){
...
}
And it is better to detect if the user is logged in or not. Like so:
function ...
8
votes
How to modify navigation menu of the "My Account" page in WooCommerce
Well customizing Woocommerce account and adding new items comes in a few steps,
First Step: Create Links:
You should use woocommerce_account_menu_items filter to modify existing menu items or adding ...
7
votes
Get the children of the parent category
Use the code below in your archive.php file.
This code will help you:
$term = get_queried_object();
$children = get_terms( $term->taxonomy, array(
'parent' => $term->term_id,
'...
7
votes
How to add menu support to a theme?
Put this pretty much anywhere (ie. functions.php)
add_theme_support( 'menus' );
6
votes
Remove current_page_parent nav class from blog index when in CPT
The current answer is great but it assumes the title of blog's navigation item is "Blog". This could cause problems if the a user ever modifies the nav item in WordPress. The following code is ...
6
votes
Displaying Logged-In User Name in Wordpress Menu
No shortcode needed
add_filter( 'wp_nav_menu_objects', 'my_dynamic_menu_items' );
function my_dynamic_menu_items( $menu_items ) {
foreach ( $menu_items as $menu_item ) {
if ( strpos($...
6
votes
Accepted
PHP 7 - Class Method Compatibility Issue
You must have the same function arguments in functions on class inheritance.
So replace function update() with update($new_instance, $old_instance) in your code
6
votes
Accepted
Add class to active top level (grandparent) menu item
Styling the top-level item if it's active or a child is active is possible with pure CSS. Use the child selector, >, from the top:
.sidebar-navigation > ul > .current-menu-item,
.sidebar-...
5
votes
Accepted
Get menus created with menu editor?
Maybe this helps :
function get_all_wordpress_menus(){
return get_terms( 'nav_menu', array( 'hide_empty' => true ) );
}
get_registered_nav_menus only gets the theme's menu's and not the ...
5
votes
Move WP Admin Bar
The admin bar is hooked to wp_footer. So …
remove_action( 'wp_footer', 'wp_admin_bar_render', 0 );
… will remove it from there. And then you can register it for a custom hook with:
add_action( '...
4
votes
Add custom classes to anchor in wp_nav_menu
I have solution to add class to anchor tag.
1: Step: add this in functions.php
function add_additional_class_on_a($classes, $item, $args)
{
if (isset($args->add_a_class)) {
$classes['...
4
votes
wp_nav_menu: check if the list item has children and add a class to anchor link
The other way is very simple and may be useful for some people
/**
* Custom Nav Classes
* https://v123.tw
*/
add_filter('nav_menu_css_class' , 'v123_nav_class' , 10 , 2 );
function v123_nav_class ($...
4
votes
How to add text before WordPress menu item href
You'll want to use the following filter:
nav_menu_link_attributes
Example:
function filter_nav_menu_link_attributes($atts, $item, $args) {
if ( isset($args->theme_location) && $args-...
4
votes
Accepted
Third level navigation class
You don't need to write your custom Walker for that...
Let's take a look on built-in Walker_Nav_Menu. You'll find this function:
public function start_lvl( &$output, $depth = 0, $args = array() )...
4
votes
Accepted
add another html tag after li element in wp_nav_menu
When using wp_nav_menu() you can exclude the <ul> tag by setting the items_wrap to only include the list items (%3$s), then you can add your own <li> before or after that, and wrap it with ...
3
votes
Navigation (wp_nav_menu) refuses to nest within parent element
'echo' => true needs to be 'echo' => false in your wp_nav_menu array for it to nest. From the docs you'll find
'echo' (bool) Whether to echo the menu or return it. Default true.
so what's ...
3
votes
Remove current_page_parent nav class from blog index when in CPT
Ok both answers are not very elegant because you have to enter the CPTs name manually. Here is a simple drop in snippet. It is pretty much copied from here so kudos to that site.
This snippet removes ...
3
votes
Accepted
Export navigation menu
WordPress has an Import/Export tool, but it doesn't works with nav menus only, the "All Content" option will export the menus too, but with everything else that is in your site (including posts/pages ...
3
votes
Accepted
remove specific classes from menu items
add_filter('nav_menu_css_class', 'special_nav_class', 10, 2);
function special_nav_class($classes, $item){
if(($key = array_search('fa', $classes)) !== false) {
unset($classes[$key]);
}...
3
votes
Accepted
how to change Wordpress menu markup/classes
Changing the CSS is the simplest way to apply your desired styles.
Another alternative, as Jack mentioned, is to create a custom walker. The Codex has a good overview.
The third option is a happy ...
3
votes
Add Bootstrap dropdown class to a nav menu
The easiest way to do this is to use an off-the-shelf solution. There is a WP_Bootstrap_Navwalker class which extends WordPress' native Walker_Nav_Menu class and makes your Navigation Menus ready for ...
3
votes
Accepted
Why is 'nav_menu_item_args' filter's $item argument empty?
Filters (and actions) need to be declared with the number of arguments accepted by the callback; it is the fourth parameter of add_filter() (default 1):
add_filter( 'nav_menu_item_args', '...
3
votes
Accepted
Return only top-level navigation items from a menu using wp_get_nav_menu_items
Let's take a look at wp_get_nav_menu_items code reference.
It takes two parameters:
$menu - (int|string|WP_Term) (Required) Menu ID, slug, name, or object,
$args - (array) (Optional) Arguments to ...
3
votes
append to existing parent theme function
No, it doesn't appear to be possible. At least not directly. You can replace functions in parent themes if they are wrapped in:
if ( ! function_exists( 'get_nav_markup' ) ) {
}
Because child themes ...
3
votes
Next/Previous Links in same category
previous_post_link takes 5 params, but you use only 2 of them.
Let's take a look at other 3:
in_same_term (boolean) (optional) Indicates whether previous post must
be within the same taxonomy ...
3
votes
Accepted
HTML Bootstrap navigation menu to Wordpress menu using wp_nav_menu
You have to edit the files in:
wordpress/wp-content/themes/twentyseventeen/template-parts/navigation/navigation-top.php
and then create a custom walker, try to take a look here.
Try in this way:
&...
Only top scored, non community-wiki answers of a minimum length are eligible
Related Tags
navigation × 905menus × 502
walker × 90
custom-post-types × 60
theme-development × 55
php × 47
sub-menu × 47
posts × 45
pages × 44
pagination × 40
categories × 38
css × 36
customization × 35
functions × 32
twitter-bootstrap × 22
next-post-link × 22
wp-query × 21
links × 21
wp-list-pages × 21
plugins × 19
themes × 19
previous-post-link × 18
loop × 17
custom-taxonomy × 15
permalinks × 14