I want to create a submenu in a page I can access from my header menu.
I created the submenu, I added a line in my "register_nav_menus".
The page I want the submenu to be has the code with the theme_location corresponding to the code added to "register_nav_menus".
Now my header menu is the submenu I just created. (I updated the location in menu's admin page).
I don't really what I can do to fix that problem. I'm pretty new to WP.
Here's the code:
FUNCTIONS.PHP
// Theme Setup
function mairie_setup() {
// Navigation resgister
register_nav_menus(
array(
'header' => __('Menu principal'),
'subnav' => __('Sous menu')
)
);
}
add_action('after_setup_theme', 'mairie_setup');
HEADER.PHP
...
<nav class="navbar">
<?php
$args = array(
'theme_location' => 'header'
);
wp_nav_menu() ?>
</nav>
...
THEME FOR SUBNAV
...
<nav class="sub_navbar">
<?php
$args = array(
'theme_location' => 'subnav'
);
wp_nav_menu() ?>
</nav>
...
I'd like my header menu to stay the header menu and not change if I create a new menu.
<nav class="navbar">
<?php
$args = array(
'theme_location' => 'header'
);
wp_nav_menu($args);
?>
</nav>
<nav class="sub_navbar">
<?php
$argss = array(
'theme_location' => 'subnav'
);
wp_nav_menu($argss);
?>
</nav>
please try this one. and read more about https://developer.wordpress.org/reference/functions/wp_nav_menu/
Related
First time building a Wordpress plugin. I create a page and I want to assign it a page template from my plugin folder. My code shows the Template name in the selectable list in the page editor, but it is not attached to the page. Nor manually attaching does anything to the page. But the path for the file is correct.
My code to create a page:
$page_path = "this-is-a-campaign-landing-page";
$page_title = 'This is a Campaigns Page Title';
$page_content = 'THIS IS A CAMPAIGNS PAGE BODY';
$page_check = get_page_by_title( $page_path );
$page = array(
'post_type' => 'page',
'post_title' => $page_title,
'post_content' => $page_content,
'post_status' => 'publish',
'post_author' => $author->ID,
'post_slug' => $page_path
);
if (!isset($page_check->ID) && !get_page_by_path($page_path)) {
$page_id = wp_insert_post($page);
if ($page_id) {
$template = '/home/vagrant/src/wptest/wp-content/plugins/pm/campaign_page.php';
update_post_meta($page_id, '_wp_page_template', $template );
}
}
Separately I add the template with add_filters
function add_campaign_template ($templates) {
$templates['campaign_page.php'] = 'Campaign Page';
return $templates;
}
add_filter ('theme_page_templates', 'add_campaign_template');
function set_campaign_template ($template) {
if ('campaign_page.php' == basename ($template)) {
$template = '/home/vagrant/src/wptest/wp-content/plugins/pm/campaign_page.php';
}
return $template;
}
add_filter ('page_template', 'set_campaign_template');
The page creates with no errors. When I view or edit the page, Default template is selected and my template appears in the list. Selecting it manually has no effect. What did I miss?
My simple page template:
<?php
/**
* Template Name: Campaign Page
*
* #package PM
*/
// get_header();
?>
<div id="primary" class="content-area">
<main id="main" class="site-main">
<section class="outer-categories">
<div class="container-fluid">
<div class="row text-justify">
THIS IS THE RIGHT PAGE TEMPLATE
THIS IS THE RIGHT PAGE TEMPLATE
THIS IS THE RIGHT PAGE TEMPLATE
THIS IS THE RIGHT PAGE TEMPLATE
<div class="col-lg-12">
<?php
while ( have_posts() ) :
the_post();
get_template_part( 'template-parts/content', 'page' );
endwhile; // End of the loop.
?>
</div>
</div>
</div>
</section>
</main><!-- #main -->
</div><!-- #primary -->
<?php
// get_footer();
Why is it not adding the template? With debug on I get no error in debug.log
Wow, long day.
Change this:
function set_campaign_template ($template) {
if ('campaign_page.php' == basename ($template)) {
$template = '/home/vagrant/src/wptest/wp-content/plugins/pm/campaign_page.php';
}
return $template;
}
add_filter ('page_template', 'set_campaign_template');
to this:
function set_campaign_template ($template) {
if ('campaign_page.php' == basename ($template)) {
$template = 'campaign_page.php'; <---- FIX
}
return $template;
}
add_filter ('page_template', 'set_campaign_template');
In Wordpress, how can I add a button or div into all sub-menu li's using wp_nav_menu?
This is my current code:
<?php wp_nav_menu(array(
'theme_location' => 'main_menu',
'items_wrap'=>'%3$s',
'container' => false
)); ?>
This is my desired output:
<li class="submenu">
<a>Link 1</a>
<ul>
<li><a>Link 2</a></li>
</ul>
<button type="button">Click Me!</button>
</li>
So, Custom Walkers are a bit of a pain to work with, until you understand them.
The below custom walker code should get you what you need. Add this to your theme's functions.php file:
class Custom_Button_Walker extends Walker_Nav_Menu {
// We only care about the "end level" part of the menu, where closing </ul> tags are generated
public function end_lvl( &$output, $depth = 0, $args = array() ) {
// This is from WP core code
$indent = str_repeat("\t", $depth);
// This line ensures we only add it on the proper level
$button = (0 == $depth) ? "{$indent}<button type=\"button\">Click Me!</button>\n" : '';
// This line is modified to include the button markup
$output .= "{$indent}</ul>\n{$button}";
}
}
To use the custom walker, modify your wp_nav_menu call like so:
wp_nav_menu( array(
'theme_location' => 'main_menu',
'items_wrap' =>'%3$s',
'container' => FALSE,
'walker' => new Custom_Button_Walker()
));
Hi I am using wordpress for the first time. I am trying to incorperated the walker class into my project which is located at
D:\wamp\www\SgsOnline\wp-content\themes\storefront\inc\functions\walker.php
I've included my class in my functions as
require get_template_directory() . '/inc/functions/walker.php';
And I am trying to call it in my header like :
function storefront_primary_navigation() {
?>
<nav id="site-navigation" class="main-navigation " role="navigation" aria-label="<?php esc_html_e( 'Primary Navigation', 'storefront' ); ?>">
<button class="menu-toggle" aria-controls="primary-navigation" aria-expanded="false"><?php echo esc_attr( apply_filters( 'storefront_menu_toggle_text', __( 'Navigation', 'storefront' ) ) ); ?></button>
<?php
wp_nav_menu(
array(
'theme_location' => 'primary',
'container_class' => 'primary-navigation',
'menu_class'=>'nav navbar-nav navbar-left nav-tabs',
'walker' => new Walker_Nav_Primary()
)
);
?>
</nav><!-- #site-navigation -->
<?php
}
But I am getting a error :
Fatal error: Class 'Walker_Nav_Primary' not found in
D:\wamp\www\SgsOnline\wp-content\themes\storefront\inc\structure\header.php
on line 65
This is oblivious wrong because it is looking in the wrong place for the class.
If anyone has any experience with this would be great
Regards
UPDATE
here is the class code
<?php
/* Collection of walker classes*/
class Walker_Nav_Primary extends Walker_Nav_menu {
function start_lvl( &$output, $depth){ //ul
$indent = str_repeat("\t", $depth );
$submenu = ($depth > 0) 'sub-menu' : ''; //Detect if the lvls is a submenu
$output .= "\n$indent<ul class=\"dropdown-menu$submenu depth_$depth\">\n";
}
/*
function end_lvl(){ // close ul
}
Not used at the moment
function start_el(){ // li, a, span
}
function end_el(){ // closing li, a, span
}
*/
}
At first glance it appears that file path is incorrect. / is unix directory separator but you need to use \ for Windows.
$path_elements = array(get_template_directory(), 'inc', 'functions', 'walker.php');
require join(DIRECTORY_SEPARATOR , $path_elements);
Walker_Nav_Primary has a syntax error in shorthand if-else statement (missing question mark after logical expression).
$submenu = ($depth > 0) ? 'sub-menu' : '';
I believe the issue is how you reference your walker...
'walker' => new Walker_Nav_Primary()
should just be...
'walker' => new Walker_Nav_Primary
no () needed.
If there was an issue with how you include your walker.php file that would error first as functions.php is run before your header.php.
Looking for your help and advices.
I have a list of links to single in Wordpress. I need to place class active only to li of active page.
Should be like on this image
But it is:
My wp-code:
<ul class="inline-list medium-6 large-4 skill-items">
<?php
$id = get_the_ID();
// echo $id;
$class;
$skills = new WP_Query( array(
'post_type' => 'skills',
'order' => 'ASC'
));
if ($skills->have_posts()) : while ($skills->have_posts()) : $skills->the_post();
// echo $post->ID;
if( $id == $post->ID) {$class = 'active';} else {$class = '';}
?>
<li class="<?php echo $class; ?>"><?php the_title(); ?></li>
<?php endwhile; endif; wp_reset_query();
?>
</ul>
That's not the proper way to do a menu in Wordpress. You should use wp_nav_menu instead of doing a custom WP_Query.
First, in functions.php add the following to register a new menu:
add_action('after_setup_theme', 'register_my_menu');
function register_my_menu() {
register_nav_menu('skills_menu', __( 'Skills menu', 'your-theme-name' ));
}
Then log in your administration, you will see that new menu placement under Appearance > Menu. Create a new menu for that placement - you have the possibility to automatically add top level new pages to this menu.
Then in your template add the following in order to display the menu:
<?php wp_nav_menu(array('theme_location' => 'skills_menu')); ?>
Wordpress will automatically handle the active class by adding current-menu-item to the appropriate li. No need for any filter for that.
Found an answer for my question using Javascript/jQuery
jQuery(document).ready(function($){
var pgurl = window.location.href;
$("ul.skill-items li").each(function(){
if($(this).find('a').attr("href") == pgurl || $(this).find('a').attr("href") == '' )
$(this).addClass("active");
})
});
pgurl contains your current url. After that for each item we are looking for anchor and its link. Then we are comapring those links, and if their are equal, we add class active to li
You may try this:
function my_alter_classes_on_li( $classes, $item ){
global $post;
if( in_array( $post->post_type, $classes ) ) $classes[] = 'current-menu-item';
return $classes;
}
add_filter( 'nav_menu_css_class', 'my_alter_classes_on_li', 10, 2 );
I wanted to add some class name of my wordpress custom menu that I created
<?php
wp_nav_menu( array(
'theme_location' => 'social-menu'
));
?>
function register_main_menus() {
register_nav_menus(array(
'social-menu' => __('Social Menu', 'sm')
));<br>
}`
`add_action('init', 'register_main_menus');`
to something like this.. I can see the option to add title attributes in admin panel but not class. How can I achieve to get those class name in each of them?
<ul>
<li></li>
<li></li>
<li></li>
</ul>
just got this one working!
function add_menuclass($ulclass) {
return preg_replace('/<a title="social-youtube-icon"/', '<a title="social-youtube-icon" class="social-youtube-icon"', $ulclass, 1);
}
add_filter('wp_nav_menu','add_menuclass');