Add # to submenu URLs automatically

This ended up doing the trick. function auto_hashtag( $menu_item ) { if ( ! is_admin() && $menu_item->post_parent > 0) { // url $url = $menu_item->url; $menu_item->url = preg_replace(‘!/([^/]+)/$!’, “/#\\1”, $url); } return $menu_item; } add_filter( ‘wp_setup_nav_menu_item’, ‘auto_hashtag’ );

how to add a div inside wp_page_menu

The question is light on detail but I’d lean toward a custom Walker: class My_Page_Walker extends Walker_Page { function end_el( &$output, $page, $depth = 0, $args = array() ) { $output .= ‘<span>After the Anchor</span></li>’; } } $args = array( ‘walker’ => new My_Page_Walker ); wp_list_pages( $args ); That should insert content just before the … Read more

Generate Navbar In WordPress

You could use Javascript to move the Submenu items where ever you please. <script> jQuery(document).ready(function() { (function ($) { $(‘.sub-menu-new’).after( $(‘.main-menu’) ); })(jQuery); }); </script> OR… You could take a look at the wp_nav_menu_objects filter hook It could allow you to do a check against every menu item and maybe store the submenu items somewhere … Read more

current_page_item is missing inside wp_nav_menu

If you’re not actually using a custom nav menu, why are you not just outputting wp_page_menu() directly? Problem As-implemented, your walker will never be executed (or, more accurately, you can’t control whether or not it is executed), because you’re not passing ‘theme_location’ (correct usage) or ‘menu’ (sub-optimal usage) to the args array. Here’s how the … Read more

Programmatically add posts from specific category to menu

I’m not 100% sure if I get you right, but maybe this will solve your problem: add_filter( ‘wp_nav_menu_items’, ‘fx_user_menu’, 10, 2 ); function fx_user_menu( $items, $args ) { if ($args->theme_location == ‘primary’) { $myposts = get_posts( array( ‘category’ => <YOUR CATEGORY ID GOES HERE>, ‘posts_per_page’ => 5 ) ); foreach ( $myposts as $post ) … Read more

Custom HTML output for registered navigation

The wp_get_nav_menu_items() function returns an indexed array of WP_Post objects. Here is an example: $menu_name=”custom_menu_slug”; $menu_locations = get_nav_menu_locations() $menu = wp_get_nav_menu_object( $menu_locations[ $menu_name ] ); $menu_items = wp_get_nav_menu_items( $menu->term_id ); $menu_list=”<ul id=”menu-” . $menu_name . ‘”>’; foreach ( (array) $menu_items as $key => $menu_item ) { $menu_list .= ‘<li><a href=”‘ . $menu_item->url . ‘”>’ . … Read more

How to get main menu only with wp_nav_menu

You can use the depth parameter like this: wp_nav_menu( array( ‘menu’ => ‘header-uk’, ‘menu_id’ => ‘nav’, ‘menu_class’ => ‘sitemap’, ‘fallback_cb’ => false, ‘depth’ => 1, ) ); Or if what you want to do is grab all items to create your own loop/output you can do that as well. Example: if ( ( $locations = … Read more

How to get current page nearest parent id?

try this. global $post; // if outside the loop $page_object = get_queried_object(); $page_id = get_queried_object_id(); // Get current page id $args = array( ‘sort_order’ => ‘ASC’, ‘sort_column’ => ‘post_title’, ‘hierarchical’ => 1, ‘exclude’ => ”, ‘include’ => $page_id, ‘meta_key’ => ”, ‘meta_value’ => ”, ‘authors’ => ”, ‘child_of’ => 0, ‘parent’ => -1, ‘exclude_tree’ => … Read more

Add a string after each item in WordPress sub menu

You can do this in three ways: 1) Use before and after arguments in wp_nav_menu 2) Write a custom walker 3) Use CSS :before or :after pseudo classes to add the character. The character must be in escaped unicode, e.g.: li:after { content: ‘\00BB’; padding-right: 5px; } There is more in a similar StackExchange answer

How to make top level menu think it has a current item

Would jQuery .parent() be an option for you? jQuery(‘.current_page_item’).parent().addClass(“classname”); If not you could write your own iteration code for the nav menus and check the various conditions as you assemble the list, but I’m guessing this would be a lot easier.