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!
Related
I am trying to pull menu on my header.php using following code:
wp_nav_menu( array(
'theme_location' => 'main-menu',
'menu_id' => 'main-menu',
) );
And in my admin login Appearance > Menu, main-menu is in following order:
Home
About
Tours
Pages
Contact Us
Help Desk
Gallery
Destinations
Blog
Booking
But in header the order is jumbled and even doesn't show the submenu also.
Order on header
About
Pages
Tours
Booking
Home
Blog
Destinations
Help me out! I even tried using order_by/sort_column, they didn't work for me.
I think Below code is what you looking for.
Add your Menu Name Instead of 'Main menu'
<?php
$main_menu = wp_get_nav_menu_items('Main menu');
$customize_arr = array();
if(!empty($main_menu)){
foreach($main_menu as $mm){
$mm = (array) $mm;
if($mm['menu_item_parent'] == 0){
foreach($main_menu as $sm){
$sm = (array) $sm;
if($mm['ID'] == $sm['menu_item_parent']){
(array)$mm['submenu'][] = $sm;
}
}
$customize_array[] = $mm;
}
}
}
?>
In $customize_array you will get all the menus with sub-menu you just have to do foreach loop with your html code.
100% Working code..I hope the Code Will help You!
If you developed the theme from scratch then add the following as an index of your array
'orderby' => 'menu_order'
Would be somthing like below
wp_nav_menu( array(
'theme_location' => 'main-menu',
'orderby' => 'menu_order'
'menu_id' => 'main-menu',
) );
If you are working on an already developed them then the issue is with 'theme_location' index of the array, so change it to 'theme_location' => 'primary' it will be something like below:
wp_nav_menu( array(
'theme_location' => 'primary',
'menu_id' => 'main-menu',
) );
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 currently have two menus created via the back end. I need to activate the menu based on the geo location, which I have aleady setup to set a default currency. The menu names in the backend are Main and Main International. Below is the code:
function geo_client_currency($client_currency){
$userInfo = geoip_detect2_get_info_from_current_ip();
if ($userInfo->country->isoCode == 'US'){
$client_currency = 'USD'; //currency code
}
else {
$client_currency = 'INR';
}
return $client_currency;
}
So essentially what I need to do is set the Main menu for the US and Main International for anywhere outside the US. I have reviewed the Codex but not quite sure how to implement that the easiest way possible
<?php
$userInfo = geoip_detect2_get_info_from_current_ip();
if ( $userInfo->country->isoCode == 'US' ){
wp_nav_menu( array( 'theme_location' => 'nav-menu', 'menu'=> 'Main' , 'depth' => 3, 'container' => false, 'menu_class' => 'sf-menu', 'walker' => new thb_MegaMenu ) );
} else {
wp_nav_menu( array( 'theme_location' => 'nav-menu', 'menu'=> 'Main International' , 'depth' => 3, 'container' => false, 'menu_class' => 'sf-menu', 'walker' => new thb_MegaMenu ) ); //Change 2 to be the Main International ID
}
?>
Could it be something as easy as this in the template file (like header.php)?
<?php
$userInfo = geoip_detect2_get_info_from_current_ip();
if ( $userInfo->country->isoCode == 'US' ){
wp_nav_menu(array('menu' => 1)); //Change 1 to be the Main ID
} else {
wp_nav_menu(array('menu' => 2)); //Change 2 to be the Main International ID
}
?>
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.
I created a theme in HTML using Zurb Foundation and then integrated it into a WordPress theme.
The primary nav section in my theme, in its most basic form is:
<dl>
<dd></dd>
<dd></dd>
<dd></dd>
</dl>
I configured wordpress's menu call as such:
wp_nav_menu( array(
'theme_location' => 'primary',
'menu' => '',
'container' => '',
'container_class' => '',
'container_id' => '',
'menu_class' => 'menu',
'menu_id' => '',
'echo' => true,
'fallback_cb' => 'wp_page_menu',
'before' => '',
'after' => '',
'link_before' => '',
'link_after' => '',
'items_wrap' => '<dl id="tabnav" class="sub-nav">%3$s</dl>',
'depth' => 0,
'walker' => '') );
But in order to change the wrapping <li> that wordpress puts out to a <dd> I would need to write a custom walker.
I looked through some custom walkers and can see parts of the logic, but is creating a class every time I want to use custom html for a menu the only/most efficient way to go about it?
digwp provides an easier approach than the walker
create a function that builds the menu from scratch.
in your functions.php
// custom menu example # http://digwp.com/2011/11/html-formatting-custom-menus/
function clean_custom_menus() {
$menu_name = 'nav-primary'; // specify custom menu slug
if (($locations = get_nav_menu_locations()) && isset($locations[$menu_name])) {
$menu = wp_get_nav_menu_object($locations[$menu_name]);
$menu_items = wp_get_nav_menu_items($menu->term_id);
$menu_list = '<dl>' ."\n";
foreach ((array) $menu_items as $key => $menu_item) {
$title = $menu_item->title;
$url = $menu_item->url;
$menu_list .= "\t\t\t\t\t". '<dd>'. $title .'</dd>' ."\n";
}
$menu_list .= "\t\t\t". '</dl>' ."\n";
} else {
// $menu_list = '<!-- no list defined -->';
}
echo $menu_list;
}
and to call it in your theme:
<?php if (function_exists(clean_custom_menus())) clean_custom_menus(); ?>
so you would have to edit the $menu_name to your menu slug.
here's the source