Adding Menu Support to Custom Wordpress Theme - wordpress

I am creating my first Wordpress theme and I am struggling with menu support:
I added a custom menu in functions.php and implemented it into my header.php like shown below but the menu-option in the administration area does not show up!
# functions.php
<?php
add_theme_support( 'menus' );
add_action( 'init', 'register_my_menus' );
function register_my_menus() {
register_nav_menus(
array(
'primary-menu' => __( 'Primary Menu' ),
'secondary-menu' => __( 'Secondary Menu' )
)
);
}
?>
# header.php
# [...]
<?php wp_nav_menu( array( 'theme_location' => 'primary-menu' ) ); ?>
# [...]
My Setting:
Wordpress Version 3.4.2
MAMP Development Environment
No plugins
Other information:
The menu option shows up in other templates
The menu is getting rendered correctly on the page
What am I missing here?
Edit #1
I can't even see the menu option in the admin menu (like here!)

Few things - You don't need add_theme_support(); nor the add_action('init', 'register_my_menus')
Just straight up call the register_nav_menus function, like so:
register_nav_menus(
array(
'primary-menu' => __( 'Primary Menu' ),
'secondary-menu' => __( 'Secondary Menu' )
)
);
Can also check if the function exists if you desire. But if it's only for use on your own theme and you know it exists it's not really needed.
if ( function_exists( 'register_nav_menus' ) ) {
...
}

Related

Add a secondary menu to my wordpress website

I am trying to add a secondary menu to my Wordpress website. My goal is to display it at the very top of my website (https://cadinwebsite.wpcomstaging.com/), above the logo.
I have installed code snippets plugin and inserted the following code:
function wpb_custom_new_menu() {
register_nav_menu('my-custom-menu',__( 'My Custom Menu' ));
}
add_action( 'init', 'wpb_custom_new_menu' );
The code above works correctly and creates a new menu location in my "menus" dashboard.
But then, when I add the code below to display the newly created menu, my website crashes.
<?php
wp_nav_menu( array(
'theme_location' => 'my-custom-menu',
'container_class' => 'custom-menu-class' ) );
?>
I also tried to input the second part of my code in the header section, but it doesn't work either. What I am doing wrong?
We have checked and you made mistake in your add action code, please check with below code, and try with that
add_action( 'after_setup_theme', 'register_my_menu' );
function register_my_menu() {
register_nav_menu( 'my-custom-menu', __( 'My Custom Menu', 'theme-text-domain' ) );
}
wp_nav_menu( array(
'theme_location' => 'my-custom-menu',
'container_class' => 'custom-menu-class'
) );

How do I create a shortcode for a WordPress sidebar?

I have added a custom sidebar to my WordPress installation using the following script that also creates a shortcode:
// Register Sidebars
function custom_sidebars() {
$args = array(
'id' => 'gutenbar',
'class' => 'abeng',
'name' => __( 'Abeng sidebar', 'text_domain' ),
'description' => __( 'Sidebar for block', 'text_domain' ),
);
register_sidebar( $args );
}
add_action( 'widgets_init', 'custom_sidebars' );
add_shortcode( 'add_sidebar', 'custom_sidebars' );
When I add the [add_sidebar] shortcode to a Gutenberg block the sidebar does not appear. However, when I use a plugin called Sidebar Shortcode [sidebar id="gutenbar"] the sidebar is displayed perfectly. I assume that my shortcode is associated with a specific function and does not need a name or id for the sidebar to be chosen.
I had been thinking that because the sidebar was added using a plugin that allows the insertion of functions available site-wide rather than only in a theme using its functions.php, that was the reason the shortcode did not do its job. But since the sidebar shows in the widgets area and allowed me to add widgets, and now works with this plugin, that's obviously not the issue.
Looking under the hood of the concisely written Sidebar Shortcode, I can't see what I might want to add to make my code functional.
Why am I doing this? I'm using full-width pages for my posts, disabling the theme's default right sidebar (which doesn't play well in a responsive design) but still need to reinsert the sidebar in a column.
Thanks for any help.
With the help of Prashant Singh on the WordPress support forum, I learned that the function that creates the sidebar cannot be used to create a shortcode to place it on a page. The shortcode has to be created calling the WP native function dynamic_sidebar() that gives access to all sidebars (https://developer.wordpress.org/reference/functions/dynamic_sidebar/). Below is the full script that includes cleanup code to allow the correct positioning inside a page.
/**
* Create sidebar and shortcode
*/
// Register Sidebars
function custom_sidebars() {
$args = array(
'id' => 'gutenbar',
'class' => '',
'name' => __( 'Abeng sidebar', 'text_domain' ),
'description' => __( 'Sidebar for block', 'text_domain' ),
);
register_sidebar( $args );
}
add_action( 'widgets_init', 'custom_sidebars' );
/* Add the shortcode and allow positioning in page*/
add_shortcode( 'add_sidebar', 'my_custom_sidebar' );
function my_custom_sidebar(){
ob_start();
dynamic_sidebar( 'gutenbar');
$sidebar_left = ob_get_clean();
$html = ' <div class="sidebar-content"> ' . $sidebar_left . ' </div> ';
return $html;
}

Wordpress Menu missing in Appearance bar

I wanted to make custom WordPress theme from scratch so i deleted all my themes, and now the Menu is missing from appearance bar,
i tried adding add_theme_support( 'menus' ); code in functions.php file then my page itself is not opening.
Attached is the screenshot for your reference.
pls help
You need to registered menu using register_nav_menus() in after_setup_theme action hook.
Please check below code. it help you to create two type of menu display location
function custom_theme_setup() {
register_nav_menus( array(
'primary' => esc_html__( 'Primary Menu', 'nepalbuzz' ),
'footer' => esc_html__( 'Footer Menu', 'nepalbuzz' ),
) );
}
add_action( 'after_setup_theme', 'custom_theme_setup' );
This code goes to your custom theme function.php file
For display menu used wp_nav_menu() function.
if ( has_nav_menu( 'primary' ) ) {
$args = array(
'theme_location' => 'primary',
'menu_class' => 'menu nepalbuzz-nav-menu',
'container' => false
);
wp_nav_menu( $args );
}
This code goes to your custom theme file where you want to display menu
Use this in your functions.php
register_nav_menus(
array('header_menu' =>'This is Header Menu'));
to display this menu use this code where you want to display your menu
wp_nav_menu(array('theme_location'=>'header_menu','container'=>''));
Please use register_nav_menus()
So that you can create a custom Menu for your theme. Like
function register_theme_menu() {
register_nav_menu( 'primary', __( 'Primary Menu', 'theme-slug' ) );
}
After that add the menu by "after_setup_theme" hook like
add_action( 'after_setup_theme', 'register_theme_menu' );
The full code is like in you fuctions.php file.
add_action( 'after_setup_theme', 'register_theme_menu' );
function register_theme_menu() {
register_nav_menu( 'primary', __( 'Primary Menu', 'theme-slug' ) );
}
get full info from https://codex.wordpress.org/Function_Reference/register_nav_menu

Unable to get wordpress register_nav_menus working

I am building my 1st theme, and got stucked with showing menu option on admin's screen under appearance. I refered various support thread but none of the solution is working for me.
Codes in function.php
<?php
//Create Nav menu
if (function_exists(register_nav_menus)) {
register_nav_menus( array('primary' => 'Header Navigation') );
}
?>
Codes in header.php
<?php
wp_nav_menu( array( 'container_class' => 'main-nav' , 'container'=>'nav') );
?>
I am executing this on localserver and wordpress version 4.0
image of header.php
http://i.imgur.com/ziPGH2G.png
image of function.php
http://i.imgur.com/ica5VJx.png
Thats all of it.
Please help me out.
First rename function.php to functions.php , note the the "s". http://codex.wordpress.org/Functions_File_Explained
Second try registering the menu inside a hook like init or after_theme_setup
add_action( 'after_setup_theme', 'register_my_menu' );
function register_my_menu() {
register_nav_menu( 'primary', 'Header Navigation' );
}
You're missing some arguments (and a comma in your array). You need to pass in a menu name:
$args = array(
'menu' => 'primary',
'container' => 'nav',
'container_class' => 'main-nav',
);
wp_nav_menu( $args );
Also be aware that this menu must exist in the admin, and must be assigned to the menu location you just created.

Insert another menu into WordPress... not working at all?

I am following this guide to "Add additional support for menus in your theme":
http://codex.wordpress.org/Navigation_Menus
I followed all of the steps, but this is the result:
http://puu.sh/30bMt.png
So, it's "inserting a menu" where I told it to.... however the items on the menu do not match up with what I have in the WordPress back-end, seen here...
http://puu.sh/30bQd.png
I only inserted 4 items into the menu "Test"... but it's displaying every page, instead of the 4 items I want.
I've attempted to do research to figure out what's going on to no avail; does anybody have any insight as to why this is acting funky?
Code where I'm "registering" the additional menu support... (themes function.php):
function register_my_menus() {
register_nav_menus(
array(
'header-menu' => __( 'Header Menu' )
)
);
}
add_action( 'init', 'register_my_menus' );
Code where I'm inserting the "Header Menu" itself... (themes header.php):
<?php
wp_nav_menu( array( 'theme_location' => 'extra-menu', 'container_class' =>'my_extra_menu_class' ) );
?>
If you are trying to call 'Header Menu' then the code in your header.php should look like this:
<?php wp_nav_menu(array('theme_location' => 'header-menu', 'menu_class' => 'my_extra_menu_class')); ?>
I'm not sure where you got 'extra-menu' from but WordPress doesn't know what that is since you didn't declare that in your register_my_menus function.
I hope that helps.
Here's an example of how I've implemented multiple menus in my WordPress install:
// Register Extra Menus
function new_register_menus() {
register_nav_menus(array('Products'=>'Products Nav', 'Support'=>'Support Nav', 'Company'=>'Company Nav' , 'Footer'=>'Footer Nav'));
}
add_action( 'init' , 'new_register_menus' );
//Code in my footer.php
<?php wp_nav_menu(array('theme_location' => 'Footer', 'menu_class' => 'nav')); ?>

Resources