I'm on my way to build my first Wordpress theme. Now I hit a problem with my menu. It is shown in the source code, but not on the website.
My code in the functions.php
function register_theme_menus() {
register_nav_menus(
array(
'primary-menu' => __( 'Primary Menu' )
)
);
}
add_action( 'init', 'register_theme_menus' );
My code in the header-php
<?php
$defaults = array(
'container' => false,
'theme_location' => 'primary-menu',
'menu_class' => 'no-bullet'
);
wp_nav_menu( $defaults );
?>
What's wrong here. Any ideas?
Thanks!
Try this page:
https://teamtreehouse.com/community/menu-not-appearing
I also found this page that mentions some context: https://codex.wordpress.org/Function_Reference/register_nav_menus
wp-includes/nav-menu.php
There seems to be a few pages coming into play, so you might be just missing quick edit. I have a good feeling about that first URL though. It mentions fix for very similar if not same issue.
Your Menu is registered as of what it shows in view-source but your toggle button is not working, as your toggle button is not linked with the menu so it just refreshed the page.
Your toggle button link is like this:
<span></span>Menu
So the href link for toggle button is blank.
Check following link to know how toggle button works:
https://codepen.io/CreativeJuiz/pen/oCBxz
EDIT:
Add your <nav> tag below the h1 tag
Use the below Jquery javascript code, I just tested localy your index page and worked fine for me
<script type="text/javascript">
jQuery(".nav-toggle").click(function(event) {
jQuery("nav").toggle('in');
});
</script>
Please change your toggle code because href="" is empty you must provide a #
<a class="nav-toggle" href=""><span></span>Menu</a>
with
<a class="nav-toggle" href="#"><span></span>Menu</a>
DEMO
Related
I am using the customizr theme on wordpress and I want to edit my footer menu. On the help page I found this php code. I put it in functions.php and it does work but I want to add another menu.
add_filter('tc_credits_display', 'my_custom_credits');
function my_custom_credits(){
$credits = '';
$newline_credits = '';
return '
<div class="span4 credits">
<p> · © '.esc_attr( date( 'Y' ) ).' '.esc_attr(get_bloginfo()).' · '.($credits ? $credits : 'Designed by Themes & Co').' ·'.($newline_credits ? '<br />· '.$newline_credits.' ·' : '').'</p> </div>';
}
The 1st one is copyright, 2nd one is themes & co link, I want to add another one for terms of service.
'Terms of Service').'
How do I do it?
to add another menu you need to do two things first declare the menu in the themes function file like so:
function register_my_menu() {
register_nav_menu('header-menu',__( 'Header Menu' ));
}
add_action( 'init', 'register_my_menu' );
Where register_my_menu can be whatever you want to call the function, header-menu needs to be unique and match the following command.
Then:
<?php wp_nav_menu( array( 'theme_location' => 'header-menu' ) ); ?>
to echo out the menu in the theme files...
please read this: http://codex.wordpress.org/Navigation_Menus
I was able to register a new menu location (it shows in the admin Appearance->Menus section. But when I call wp_nav_menu with that location, it prints nothing. If I set location =>'' then it prints it!
functions.php
//This works
add_action( 'init', 'register_my_menus' );
function register_my_menus()
{
register_nav_menus( array( 'my_special_slug' => __( 'My Menu' )));
}
header.php
wp_nav_menu(
array(
'items_wrap'=> '%3$s',
'walker' => new MyWalker(),
'container'=>false,
'menu_class' => '',
'theme_location'=>'my_special_slug', //This does NOT work
'fallback_cb'=>false
)
);
What am I doing wrong?
register_nav_menu takes a comma separated array.
function register_my_menus()
{
register_nav_menu(array( 'my_special_slug', 'My Menu');
}
It appears (please correct me if this is incorrect) that without the user going into the admin interface and turning the menu on, it cannot be found. They need to go to `Appearance->Menus->Locations" and select the menu to show for that location.
I have been unable to find a programmatic way of doing this.
I am stuck and trying to figure out the Problem... in WordPress site
al-hussain
now the Problem is that,
i found a menu appearing on the home, i changed my menu..but when i clicked on pages just #
was the URL of All Pages but the page Contact-us has actual link, and the menu winch i created now appears in actual position..means when i goes to contact-us page and than my created menu is activated else,the default or static menu is appearing... even though i delete all my created menu but on loading site it shows menu...
default Menu is
and after clicking on contact us the menu is
here is header.php main_nav_menu
<!-- Begin main nav -->
<div id="nav_wrapper">
<div class="nav_wrapper_inner">
<div id="menu_border_wrapper">
<?php
if ( has_nav_menu( 'primary-menu' ) )
{
//Get page nav
wp_nav_menu(
array(
'menu_id' => 'main_menu',
'menu_class' => 'nav',
'theme_location' => 'primary-menu',
)
);
}
else
{
echo '<div class="notice">Please setup "Main Menu" using Wordpress Dashboard > Appearance > Menus</div>';
}
?>
</div>
</div>
</div>
any one can figure out ! please
Did you check your header.php file? I believe you have static menu there.
Here is the syntax to create a menu that you have created at the backend
<?php wp_nav_menu( array('menu' => 'main_menu', 'container' => '') ); ?>
Replace your static menu with the code above with the name changed in the 'menu' parameter
i sloved the Problem by Deleting the Cache, here i found directory
wp-content/cache/supercache/
thanks
Hi im a web dev an im trying to get word press to use a 2nd navigation for my blog section. i have the main site all set up but what i want is when i hit the blog in my top nav to show a differnt navigation for the blog section. i have tried using the following code in the header.php
<?php
if (is_single('blog')){
wp_nav_menu(array('menu'=>'secondary' ));
}
?>
but this code does not seem to work even though the theme supports 2 navigations.
<nav class="Seo-nav">
<div class="Seo-nav-inner">
<?php
echo theme_get_menu(array(
'source' => theme_get_option('theme_menu_source'),
'depth' => theme_get_option('theme_menu_depth'),
'menu' => 'primary-menu',
'class' => 'Seo-hmenu'
)
);
get_sidebar('nav');
?>
the above code is the code i use to call my navigation. is there any way to get a specific page or pages to show the one menu? any help on this would be great. never had to make certain pages have different navigation before so this is a new one on me.
Like was said in comments earlier, you'll need Conditional Tags. Probably for you specifically, the is_page() conditional.
<?php
if (is_page('blog')) {
// Echo this menu on the blog slug page
wp_nav_menu(array(
'theme_location' => '[name_of_menu]'
));
} else {
// Otherwise, echo this one
wp_nav_menu(array(
'theme_location' => '[name_of_main_menu]'
));
}
See the codex for the wp_nav_menu attributes. Can print out the menu other ways, that's just the one I recommend.
Depending how you blog is set up, is_page('blog') may need to be is_home(), 'blog' == get_post_type(), or some other variation.
I think you can go here two ways.
One is to only show children of current page on the second like
function show_different_nav(){
global $post;
$sec_nav=wp_list_pages("child_of=".$post->ID);
return $sec_nav;
}
now call show_different_nav() in your template.
The other way would be to write an own filter. I modified the http://wordpress.org/plugins/get-different-menus/ in this case.
add_filter( 'wp_nav_menu_items', 'get_different_custom_menu_item_according_to_page_slug', 10, 2 );
function get_different_custom_menu_item_according_to_page_slug ( $items, $args ) {
$current_id=(int)$GLOBALS['post']->ID;
$current_slug=$GLOBALS['post']->slug;
$the_nmo = wp_get_nav_menu_object($current_slug);
if ($the_nmo==""){}else{
$menu_id=$the_nmo->term_id;
$items_2 = wp_get_nav_menu_items($menu_id);
$items="";
if ($args->theme_location == 'secondary') {
foreach ($items_2 as $item){
$link_page_id = (int)($item->object_id);
if ($current_id == $link_page_id){$cur=" current-menu-item current_page_item";}else{$cur="";
}
$items .= '<li class="menu-item menu-item-type-post_type menu-item-object-page'.$cur.'">'.$item->title.'</li>';
}
}
}
return $items;
}
In my opinion you need a menu for this called exactly the same as the page slug.
I am working on a site that calls the categories of a wordpress page and displays them in the right-side navigation using a php call. I am new to php and web programming in general. Is there a way I could split the categories into two sections using a particular php call or perhaps an if-loop.
Essentially, I want to display particular categories under custom sub-headings to better organize the site. Any help, I'm currently using the following script to display my categories:
<ul><?php wp_list_categories('show_count=1&title_li='); ?></ul>
Here is my site for reference: http://www.merrimentdesign.com
Try using your code above twice. Each time, you can use the other function arguments to limit the output to certain categories. See http://codex.wordpress.org/Template_Tags/wp_list_categories for the various ways to customize the output of the function.
For example you could use:
<ul><?php wp_list_categories('show_count=1&title_li=&child_of=100'); ?></ul>
// where 100 is the parent id of all of the categories you want to print.
<ul><?php wp_list_categories('show_count=1&title_li=&exclude_tree=100'); ?></ul>
// and then show everything, but children of 100
Or simply use the first string multiple times specifying different parent ids each time.
By far and away your best option is to use the new menu functionality within WordPress. It's dead straight forward to set up in your theme:
add_theme_support( 'menus' );
add_action( 'init', 'register_my_menus' );
function register_my_menus() {
register_nav_menus(
array(
'public-menu' => __( 'Public Menu' ),
'sidebar-public-menu' => __( 'Sidebar Public Menu' ),
'sidebar-members-menu' => __( 'Sidebar Members Menu' ),
'sidebar-staff-menu' => __( 'Sidebar Staff Menu' ),
'footer-menu' => __( 'Footer Menu' )
)
);
}
place that in your functions.php file (and obviously change it for your requirements).
Then in your template file - probably sidebar.php you'll want something like:
<?php wp_nav_menu( array( 'theme_location' => 'sidebar-staff-menu', 'container' => false ) ); ?>
And then go to the back end of WordPress (your wp-admin) and then go to Appearance > Menus and voila you're able to drag and drop your categories to your heart's content!
Helpful link: http://justintadlock.com/archives/2010/06/01/goodbye-headaches-hello-menus
Read that, Justin Tadlock is awesome.
Good luck.