-
AuthorPosts
-
November 3, 2018 at 4:39 pm #1029499
Hi, I’m working to get a different menu on specific pages. Here is my code so far
add_action( 'after_setup_theme', 'register_my_menus' ); function register_my_menus() { register_nav_menus( array( 'specialmenu' => 'Special Menu' )); } add_filter('wp_nav_menu_items', 'add_login_logout_link', 10); function add_login_logout_link() { global $post; $parentid = array(85,9); if (in_array($post->post_parent, $parentid) || (in_array($post->ID, $parentid))){ wp_nav_menu( array( 'theme_location' => 'specialmenu' ) ); } }
I believe I’m hooking it to the wrong location. Where should I be hooking it to to overwrite the avia menu? Also any styling information you can add will be great too.
Please do not recommend this plugin: wordpress.org/plugins/zen-menu-logic/
It has not been updated in a year and spits out errors all over the admin panel. It also requires me to go to every page and select the menu manually. I have hundreds of pages and do not intend to do this.
Thank you for your help
- This topic was modified 6 years ago by GCSkye.
November 8, 2018 at 2:53 am #1031059Hey GCSkye,
Thank you for using Enfold.
You cannot return a whole menu when using the “wp_nav_menu_items” filter. You need to edit the includes > helper-main-menu.php file directly and load the appropriate wp_nav_menu based on your conditions.
// https://developer.wordpress.org/reference/functions/wp_nav_menu/
You can also use the “avf_alternate_mobile_menu_id” filter but it will override the default heading settings.
Best regards,
IsmaelNovember 8, 2018 at 10:44 pm #1031431Inside helper-main-menu.php I replaced:
$avia_theme_location = 'avia'; $avia_menu_class = $avia_theme_location . '-menu';
With:
global $post; $newmenu1= array(85,9); $newmenu2= array(4, 22); $newmenu3= array(5, 34); if (in_array($post->post_parent, $newmenu1) || (in_array($post->ID, $newmenu1))){ $avia_theme_location = 'newmenu1'; } else if (in_array($post->post_parent, $newmenu2) || (in_array($post->ID, $newmenu2))){ $avia_theme_location = 'newmenu2'; } else if (in_array($post->post_parent, $newmenu3) || (in_array($post->ID, $newmenu3))){ $avia_theme_location = 'newmenu3'; } else { $avia_theme_location = 'avia'; // Goes to default Enfold Main Menu } $avia_menu_class = 'avia-menu';
This allows my menus to work perfectly but the burger menu does not display unless on a page with the avia menu showing. What is causing the burger menu and search icon to not display for newmenu1, newmenu2, and newmenu3?
Note: I’ve added the following to function.php to avoid any questions about this:
// Adds new custom menus inside admin area add_action( 'after_setup_theme', 'register_gc_menus' ); function register_gc_menus() { register_nav_menus( array( 'newmenu1' => 'New Menu 1' )); register_nav_menus( array( 'newmenu2' => 'New Menu 2' )); register_nav_menus( array( 'newmenu3' => 'New Menu 3' )); }
- This reply was modified 6 years ago by GCSkye.
November 9, 2018 at 12:39 am #1031458Updated above post.
November 9, 2018 at 11:06 pm #1031861Solved. Here is the solution for those needing it.
Enfold specifically targets the ‘avia’ menu and only adds the search icon and burger menu to it. In this first step we’re going to do three things.
1. Create our custom menus and have them appear in the admin panel
2. Remove the default search icon that only targets avia and add one that targets our menus also
2. Remove the default burger icon that only targets avia and add one that targets our menus alsoCreate a file called function-gc.php and add the following code:
// Adds new custom menus inside admin area add_action( 'after_setup_theme', 'register_gc_menus' ); function register_gc_menus() { register_nav_menus( array( 'newmenu1' => 'New Menu 1' )); //name of your first menu register_nav_menus( array( 'newmenu2' => 'New Menu 2' )); //name of your second menu register_nav_menus( array( 'newmenu3' => 'New Menu 3' )); //name of your third menu; feel free to add as many of these as you'd like } /* AJAX SEARCH */ //first append search item to main menu remove_filter( 'wp_nav_menu_items', 'avia_append_search_nav', 9997, 2 ); remove_filter( 'avf_fallback_menu_items', 'avia_append_search_nav', 9997, 2 ); add_filter( 'wp_nav_menu_items', 'gc_append_search_nav', 9997, 2 ); add_filter( 'avf_fallback_menu_items', 'gc_append_search_nav', 9997, 2 ); function gc_append_search_nav ( $items, $args ) { if(avia_get_option('header_searchicon','header_searchicon') != "header_searchicon") return $items; if(avia_get_option('header_position', 'header_top') != "header_top") return $items; //edit the line below with your information (newmenu1, newmenu2, etc) if ((is_object($args) && in_array($args->theme_location, array('avia','newmenu1','newmenu2','newmenu3'))) || (is_string($args) && $args = "fallback_menu")) { global $avia_config; ob_start(); get_search_form(); $form = htmlspecialchars(ob_get_clean()) ; $items .= '<li id="menu-item-search" class="noMobile menu-item menu-item-search-dropdown menu-item-avia-special"> <a href="?s=" rel="nofollow" data-avia-search-tooltip="'.$form.'" '.av_icon_string('search').'><span class="avia_hidden_link_text">'.__('Search','avia_framework').'</span></a> </li>'; } return $items; } /* Enables burger menu on all pages */ //first append search item to main menu remove_filter( 'wp_nav_menu_items', 'avia_append_burger_menu', 9998, 2 ); remove_filter( 'avf_fallback_menu_items', 'avia_append_burger_menu', 9998, 2 ); add_filter( 'wp_nav_menu_items', 'gc_append_burger_menu', 9998, 2 ); add_filter( 'avf_fallback_menu_items', 'gc_append_burger_menu', 9998, 2 ); function gc_append_burger_menu ( $items , $args ) { global $avia_config; $location = ( is_object( $args ) && isset( $args->theme_location ) ) ? $args->theme_location : ''; $original_location = isset( $avia_config['current_menu_location_output'] ) ? $avia_config['current_menu_location_output'] : ''; /** * Allow compatibility with plugins that change menu or third party plugins to manpulate the location * * @used_by Enfold config-menu-exchange\config.php 10 * @since 4.1.3 */ $location = apply_filters( 'avf_append_burger_menu_location', $location, $original_location, $items , $args ); //edit the line below with your information (newmenu1, newmenu2, etc) if ((is_object($args) && in_array($args->theme_location, array('avia','newmenu1','newmenu2','newmenu3'))) || (is_string($args) && $args = 'fallback_menu')) { $class = avia_get_option('burger_size'); $items .= '<li class="av-burger-menu-main menu-item-avia-special '.$class.'"> <a href="#"> <span class="av-hamburger av-hamburger--spin av-js-hamburger"> <span class="av-hamburger-box"> <span class="av-hamburger-inner"></span> <strong>'.__('Menu','avia_framework').'</strong> </span> </span> </a> </li>'; } return $items; }
Now we need enfold to know our new file exists
inside function.php replacerequire_once( 'functions-enfold.php');
with
require_once( 'functions-enfold.php'); require_once( 'functions-gc.php'); // you'll need to redo this step every time you update the theme
Lastly, we want to have the menus appear on the pages they should appear on. This step will need to be repeated every time you update the theme.
Inside helper-main-menu.php replace:
$avia_theme_location = 'avia'; $avia_menu_class = $avia_theme_location . '-menu';
With:
global $post; $newmenu1= array(85,9); // numbers represent pages, categories you want to target $newmenu2= array(4, 22); $newmenu3= array(5, 34); //if statement for every menu you have with a fallback to the default main menu if (in_array($post->post_parent, $newmenu1) || (in_array($post->ID, $newmenu1))){ $avia_theme_location = 'newmenu1'; } else if (in_array($post->post_parent, $newmenu2) || (in_array($post->ID, $newmenu2))){ $avia_theme_location = 'newmenu2'; } else if (in_array($post->post_parent, $newmenu3) || (in_array($post->ID, $newmenu3))){ $avia_theme_location = 'newmenu3'; } else { $avia_theme_location = 'avia'; // Goes to default Enfold Main Menu } $avia_menu_class = 'avia-menu';
Let me know if you have any questions
- This reply was modified 6 years ago by GCSkye.
November 11, 2018 at 8:16 pm #1032300Hi GCSkye,
Glad you got it working for you and thank you for sharing! :)
If you need further assistance please let us know.
Best regards,
Victoria -
AuthorPosts
- You must be logged in to reply to this topic.