WordPress navigation menu is not loading - wordpress

I am working on my custom theme in wordpress, I have a problem in the navigation in my site, when I open the site, the main navigation is not loading.
Below are the codes.
Any assistance is highly appreciated.
Thanks.
Function.php file
function register_my_menus() {
register_nav_menus( array(
'menu-1' => esc_html__( 'Primary', 'nethub' ),
) );
}
add_action( 'init', 'register_my_menus' );
Then in header.php the code is
<?php
wp_nav_menu(
array(
'theme_location' => 'menu-1',
'menu' => 'primary-menu',
'container' => false,
'items_wrap' => '<ul class="rd-navbar-nav">%3$s</ul>',
)
);
?>
</div>

Have you created a menu in the menus edit screen and set it to the "Primary" location? Also think you can remove the 'menu' => 'primary-menu', from your header.php:
// header.php
<?php
wp_nav_menu(array(
'theme_location' => 'menu-1',
'container' => false,
'items_wrap' => '<ul class="rd-navbar-nav">%3$s</ul>',
));
?>
</div>

Related

how to add custom wordpress Menu using just A tag output

I want to create menu like below code but wordpress generate other code.
help me to generate code like below HTML code
Thanks in advance.
help me to generate below code using wordpress
You can do this by using menu walker class like this:
wp_nav_menu( array(
'container' => false,
'menu' => 'main-menu',
'menu_class' => 'nav navbar_menu',
'walker' => new Custom_Walker_Nav_Menu
)
);
add_theme_support('menu');
function register_newtheme_menu(){
register_nav_menus(
array(
'main-menu'=>_('Main Menu')
)
);
}
add_action('init','register_newtheme_menu');
function add_menuclass($ulclass) {
return preg_replace('/<a /', '<a class="navigation-link w-nav-link" ', $ulclass);
}
add_filter('wp_nav_menu','add_menuclass');
?>
<nav class="menu-hamburger w-nav-menu" role="navigation">
<?php
$defaults = array(
'container' => 'false',
'echo' => false,
'fallback_cb' => false,
//'items_wrap' => '<a id="%1$s" class="%2$s">%3$s</a>',
'depth' => 0
);
//echo wp_nav_menu( $defaults );
echo strip_tags(wp_nav_menu( $defaults ),'<a>');
?>
</nav>

How to make function to get page ancestors in wordpress

How I can get child pages for each page, if a page dont have any child then there must be shown right menu.
For Example:
Parent Page
child page 1
child page 2
if parent don't have any child then display any menu
Right now I am using following code in:
FUNCTION.PHP
function get_top_ancestor_id() {
global $post;
if ($post->post_parent) {
$ancestors = array_reverse(get_post_ancestors($post->ID));
return $ancestors[0];
}
return $post-> ID;
}
HEADER.PHP
<?php
$args = array(
'child_of' => get_top_ancestor_id(),
'title_li' => ''
);
?>
<?php wp_list_pages($args); ?>
This is the default menu calling in wordpress
<?php wp_nav_menu( array( 'theme_location' => 'primary', 'menu_class' => 'nav-menu', 'menu_id' => 'primary-menu' ) ); ?>
Directly use this function
<?php wp_nav_menu( array( 'theme_location' => 'primary', 'menu_class' => 'nav-menu', 'menu_id' => 'primary-menu' ) ); ?>
Ahmad, if you just want to list the children of the page that you're currently on, you can use the function wp_get_post_parent_id (http://codex.wordpress.org/Function_Reference/wp_get_post_parent_id)
So, your code might be simplified to
<?php
global $post;
//If the page has children - print the list
if(wp_get_post_parent_id( $post->ID ))
{
$args = array(
'child_of' => wp_get_post_parent_id( $post->ID ),
'title_li' => ''
);
wp_list_pages($args);
}
else{
$args = array(
'child_of' => $post->ID,
'title_li' => ''
);
wp_list_pages($args);
}
?>

Why do i have duplicated menu on wordpress?

Here i am creating 2 menu sites "Main" and "Central"
if ( function_exists( 'register_nav_menus' ) ) {
register_nav_menus(
array(
'main' => 'Main',
'central' => 'Central'
)
);
}
when i am creating menus on wordpress aperience, menus
and i am puting them on their sites
PRS menu on Main
and central on Central
When i have incerted my menus on web
<?php wp_nav_menu( array('menu' => 'Main','menu_class' => 'menu' )); ?>
<?php wp_nav_menu( array('menu' => 'Central','menu_class' => 'menu' )); ?>
But on my web i am seyng "central" menu 2 times and PRS dont apear.
What is the problem and what solution cn i find?
Try single Register this way & then move on to array - add code below to functions.php:
WP Codex register_nav_menu
<?php
add_action( 'after_setup_theme', 'register_my_menu' );
function register_my_menu() {
register_nav_menu( 'primary', 'Primary Menu' );
}
?>
Display menu on page - add to header.php:
WP Codex wp_nav_menu
<?php
$defaults = array(
'theme_location' => 'primary',
'menu' => 'Primary 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>',
'depth' => 0,
'walker' => ''
);
wp_nav_menu( $defaults );
?>
Tip - both of your entries have a 'menu' class:
Your code:
<?php wp_nav_menu( array('menu' => 'Main','menu_class' => 'menu' )); ?>
Change to:
<?php wp_nav_menu( array('menu' => 'Main' )); ?>

Wordpress wp_nav_menu not showing nav

trying to add a footer menu to my footer in WordPress, The menu is saved inside the database and I'm copying the same code from the header which is working perfectly. I can't see the error:
code is:
<?php
if ( has_nav_menu( 'footer_nav' ) ) {
wp_nav_menu( array('container' => '<ul>', 'menu' => 'Footer Menu', 'items_wrap' => '<li>%1$s</li>' ));
}
?>
any help would be great
You have to give the theme_location for footer_nav,
wp_nav_menu( array( 'theme_location'=>'footer_nav',
'container' => '<ul>',
'menu' => 'Footer Menu',
'items_wrap' => '<li>%1$s</li>' ));

Active menu class not working in wordpress

I am developing my own theme (using the functions of Twenty Fourteen (nothing has changed in functions.php)). My header does not contain wp_head() but I am using body_class() in my body tag. My menu is defined as so:
<?php
$menuParameters = array(
'container' => false,
'echo' => false,
'items_wrap' => '%3$s',
'depth' => 0,
);
echo strip_tags(wp_nav_menu( $menuParameters ), '<a>' );
?>
Unfortunately, my current-menu-item is not being applied to my navigation anchors when on a wordpress-created page. Am I missing a function somewhere that renders this class? Thanks in advance?
<?php wp_nav_menu(array(
'container'=> '',
'menu_class' => '',
'menu_id' => '',
)); ?>

Resources