wordpress custom menu with page thumbnail - php

I am trying to get a menu with few links, and show their/link/page featured image. following is my code for functions.php.
function register_my_menus() {
register_nav_menus(
array(
'main-menu' => __( 'Main Menu' ),
'useful-links' => __( 'Userful Links' )
)
);
}
add_action( 'init', 'register_my_menus' );
and this is where i am calling it in the page.
<?php wp_nav_menu( array( 'theme_location' => 'useful-links', 'sort_column' => 'menu_order', 'container_class' => 'useful-links', 'menu'=>'Posts Menu' ) ); ?>
i know something is missing but kindly advice. my page look and feel allow 6 links and their images.
thanks

This should be in your functions.php to register the menu within WordPress.
if (!function_exists('wp_basicTemplateSetup')) {
function wp_basicTemplateSetup() {
// Register the Main Menus into WordPress
register_nav_menus(array(
'primary' => __('Main Menu', 'theme-name'),
));
}
}
add_action('after_setup_theme', 'wp_basicTemplateSetup');
In your theme, where the menu is sitting you need to tell the menu function, which menu to look for. So..:
// Variable that tells WordPress which menu to use, look at "Theme Location"
// Put this variable either at the top of your theme, of right *before* the function wp_nav_menu
$mainmenu = array(
'theme_location' => 'primary',
'container' => false,
'menu_class' => 'some-menu-css-class',
'depth' => 2
);
// The WordPress menu function
<?php wp_nav_menu($mainmenu); ?>
These 3 components are necessary to get your WordPress menu working.

Related

Wordpress menus

i m having a problem with menus.
I had a template that wasnt originaly for WP.
so i integrated it.
when i started the site there was only one menu which works great.
i added <?php wp_nav_menu('primary'); ?> and its all ok
Now i need to add another one (custom links in the header section)
sooo i used this method
register_nav_menus( array(
'header' => 'Header menu',
'footer' => 'Footer menu'
) );
and recieved new locations for menus.
for the second menu i added
<?php wp_nav_menu( array( 'theme_location' => 'header', 'menu_class' => 'nav-menu', 'fallback_cb' => false ) ); ?>
now the problem:
there is a primary menu (it has few items)
and the custom menu ( its currently empty)
when i add new page to custom menu its being shown in both primary and custom menus.
if i delete all the items from custom menu, primary menu items are back in place...
what is the problem?
Thank you.
If I understand correctly, it looks like you are pointing to the wrong registered menu in the wp_nav_menu(). Specifically in the theme_location.
When you are building the "second menu" which is the "footer", theme_location should be footer since that is the name you gave it in the register_nav_menus().
<?php wp_nav_menu(
array( 'theme_location' => 'footer', // Change 'header' to 'footer'
'menu_class' => 'nav-menu',
'fallback_cb' => false )
);
?>

Create own footer menu in Wordpress?

I do have two menus in my Wordpress-Page which are called
Mainmenu(Primary Menu)
Footer
I associated the "Mainmenu" to the header of the website. I want the footer of the Wordpresspage to look exactly like the header, only difference should be the Links in it. It should not be taken from the "Mainmenu", but from the menu called "Footer". The function of the Header looks like this:
echo '<nav id="rtp-primary-menu" role="navigation" class="rtp-nav-wrapper' . apply_filters( 'rtp_mobile_nav_support', ' rtp-mobile-nav' ) . '">';
rtp_hook_begin_primary_menu();
/* Call wp_nav_menu() for Wordpress Navigaton with fallback wp_list_pages() if menu not set in admin panel */
if ( function_exists( 'wp_nav_menu' ) && has_nav_menu( 'primary' ) ) {
wp_nav_menu( array( 'container' => '', 'menu_class' => 'menu rtp-nav-container clearfix', 'menu_id' => 'rtp-nav-menu', 'theme_location' => 'primary', 'depth' => apply_filters( 'rtp_nav_menu_depth', 4 ) ) );
} else {
echo '<ul id="rtp-nav-menu" class="menu rtp-nav-container clearfix">';
wp_list_pages( array( 'title_li' => '', 'sort_column' => 'menu_order', 'number' => '5', 'depth' => apply_filters( 'rtp_nav_menu_depth', 4 ) ) );
echo '</ul>';
}
rtp_hook_end_primary_menu();
echo '</nav>';
The Theme i am using is rtPanel. Can anyone tell me how i simply can change this function in the way that it will load its content from the Menu called "Footer" and not from the Primary Menu which is "Mainmenu"?
Please dont worry about the CSS and everything, i will take care about this. I really just want to now how i have to change this function to load the "Footer"-Menu in it and not the Primary-Menu(Mainmenu).
As an Attachment how it look in the menu:
Thank you very much advance!
This is really simple, in fact when you start developing wordpress theme this might be one of the very first items you need to learn.
You need to register a menu location for footer seciton. You can learn more in codex
register_nav_menu( 'footer', __( 'Footer Menu', 'theme-slug' ) );
This will register the menu for you. Since you already have the menu location just find the location parameter. We're going to use that in the following code. For now assume it 'footer' like the above one. And the code for the footer menu according to your referenced code-
echo '<nav id="rtp-primary-menu" role="navigation" class="rtp-nav-wrapper' . apply_filters( 'rtp_mobile_nav_support', ' rtp-mobile-nav' ) . '">';
//rtp_hook_begin_primary_menu(); // we better disable this hook as it intends for the header primary menu
/* Call wp_nav_menu() for Wordpress Navigaton with fallback wp_list_pages() if menu not set in admin panel */
if ( function_exists( 'wp_nav_menu' ) ) {
wp_nav_menu( array( 'container' => '', 'menu_class' => 'menu rtp-nav-container clearfix', 'menu_id' => 'rtp-nav-menu', 'theme_location' => 'footer', 'depth' => apply_filters( 'rtp_nav_menu_depth', 4 ) ) );
} else {
echo '<ul id="rtp-nav-menu" class="menu rtp-nav-container clearfix">';
wp_list_pages( array( 'title_li' => '', 'sort_column' => 'menu_order', 'number' => '5', 'depth' => apply_filters( 'rtp_nav_menu_depth', 4 ) ) );
echo '</ul>';
}
//rtp_hook_end_primary_menu(); // we better disable this hook as it intends for the header primary menu
echo '</nav>';
Notice the theme_location parameter in wp_nav_menu() function? This is the one which plays behind to get you menu correctly.
Now play around with CSS and you might get what you intend.
Hope this might help you.

How can I programatically change menu in Wordpress?

I've created one menu in Norwegian menu_no and one in English menu_en.
I can see that my theme supports only one menu, but I'm not planning on using more that one menu at a time. So when user selects English language, how can I change the active menu?
I've not found anything on Google and I can't find the right function in nav-menu.php.
UPDATE
I found quite a simple solution for my problem. I just had to think a bit differently. In my functions.phpI added this code:
add_action('init', 'register_menus');
function register_menus(){
register_nav_menus( array(
'menu_no' => 'Norwegian menu',
'menu_en' => 'English menu',
) );
}
and in my header.php file I use this code:
global $lang;
$args = array(
'theme_location' => 'menu_'.$lang,
'container' => false
);
<?php wp_nav_menu($args); ?>
Voila. I'll post it as answer later - if not anyone else comes up with a better idea.
Here is How You Can Do That.
Past this code to function.php file
// Register Navigation Menus
function custom_navigation_menus() {
$locations = array(
'first_menu' => __( 'Menu for English language', 'text_domain' ),
'second_menu' => __( 'Menu for Other Language', 'text_domain' ),
);
register_nav_menus( $locations );
}
// Hook into the 'init' action
add_action( 'init', 'custom_navigation_menus' );
You can customize the above code.
Now use wp_nav_menu function to display anywhere you want like this
<?php
wp_nav_menu( array(
'menu' => 'primary',
'theme_location' => 'first_menu',
);
?>
<?php
wp_nav_menu( array(
'menu' => 'secondary',
'theme_location' => 'second_menu',
);
?>
You can display each menu in different places. Now you to Option in you menu Primary and Secondary

Need to customize the top menu in woothemes canvas, depending if the user is logged in or not I need to show 1 menu instead of the other

I've already created a canvas child-theme. I added my custom page woo-hooks.class.php in the canvas-child folder in which i copied all the code from the original and added here:
$this->hooks['header'] = array(
'woo_top' => array(
'content' => '',
'shortcodes' => 0,
'php' =>'
the following code:
if ( is_user_logged_in() ) {
wp_nav_menu( array( "theme_location" => "top-menu-loggedin" ) );
} else {
wp_nav_menu( array( "theme_location" => "top-menu-loggedout" ) );
}
the code is like it doesn't even exists, I know I've probably mistaken what I have to do, but i couldn't find a clear guide anywhere on how to do this, just some code in some forums with no context about how and where to act (the code i added I found it in a forum, but I guess i didn't add it in the right place). The filter and hooks guides of WP don't make clear WHERE and HOW to call the custom filters and hooks and how to actually make them interact with the existing code... I'm going nuts, please help.
Top menu customization can be performed in header section, that is in child theme's 'header.php' file or in other file, specifying 'theme-location' parameter.
You can write below code, in your child theme,
if (is_user_logged_in()){
wp_nav_menu(array(
'menu_class' => 'member-menu',
'menu' => 'Members',
'theme_location' => 'primary');
);
}else{
wp_nav_menu(array(
'menu_class' => 'top-menu',
'menu' => 'Members non Logged in',
'theme_location' => 'primary')
);
}
Here, 'theme-location' parameter is specifies location of Menu and 'menu' specifies name of Menu.

WP Footer Menu Not working?

I have been working on a site to change from HTML To Wordpress CMS...so got completed everything...Now stuck in one problem..and that is that I have declared the Main Navigation and Footer Navigation correctly (according to me) in functions.php file as :
/* Register Nav Main Menu */
register_nav_menus (array('main-nav' => 'Main Navigation'));
register_nav_menus (array('footer-nav' => 'Footer Navigation'));
while I am calling the both menus in correct way as for main-nav I am calling it in header.php as :
wp_nav_menu( array( 'menu' => 'main-nav' ));
while calling the footer-nav as following in footer.php as :
wp_nav_menu( array( 'menu' => 'footer-nav' ));
but in both places only main-nav is shown...so what did I do wrong please?..Need a hand to fix it...!
Waiting for your replies..!
Here is live link to the site as : http://www.huntedhunter.com/minaterra/
try changing:
wp_nav_menu( array( 'menu' => 'footer-nav' ));
to
wp_nav_menu( array( 'theme_location' => 'footer-nav' ));

Categories