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');
Related
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/
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 having an issues with the customizing of wordpress menu theme. I want to apply different classes on each of the selected menu.
This is exactly what i want.
<ul>
<li class="item1">Home Page</li>
<li class="item2">About Page</li>
<li class="item3">Contact Page</li>
</ul>
When the menu item is clicked different classes would be applied on each of the selected menu item
For example i want to click menu item1 a class active1 should be added to the selected element simillary if i clicked item2 a class active2 should be added to the selected element
This is how i am trying to achieve this but its not working
add_filter('nav_menu_css_class' , 'special_nav_class' , 10 , 2);
function special_nav_class($classes, $item){
if ($item == 'Home Page') {
if( in_array('current-menu-item', $classes) ){
$classes[] = 'active1';
}
}
if ($item == 'Home Page') {
if( in_array('current-menu-item', $classes) ){
$classes[] = 'active2';
}
}
return $classes;
}
Can Anyone please help me? Thanks in Advance
This requires some jQuery
PHP
First, let's construct the menu:
$menu = array(
'1' => 'Home',
'2' => 'About',
'3' => 'contact',
);
PHP/HTML
<ul class="menu">
<?php foreach ($menu as $id => $item): ?>
<li><a class="item item-<?= $id; ?>" href="#"><?= $item; ?></a></li>
<?php endforeach; ?>
jQuery
$(document).ready(function() {
$(document).on('click', '.item', function () {
$('.item').removeClass('active');
$(this).addClass('active');
});
});
After reading more closely I see you want to add the class in PHP. In order for me to answer your question, I need to know how the $classes array looks.
I was looking for a flexible way to generate different sidebars in different pages. The goal is to pass custom links to each sidebar. The template libraries seemed overkill for my application so I developed an easy solution. I'm not certain if it's the best solution. My question is what do you think? Your advice is immensely appreciated!
1. In your controller add a private function that loads your sidebar view:
/**
* The function has 2 arguments.
* $title is the sidebar widget title.
* $widget will contain an array of links to be added in the sidebar widget.
*/
private function sidebar($title, $widget)
{
$widget['title'] = $title;
$this->load->view('includes/sidebar', $widget);
}
2. In the controller functions that you want to load a custom sidebar, call the private function sidebar() and pass desired sidebar data into it.
Below is a controller function named edit used to edit posts. In the example I need to load a sidebar with options to view and delete the post I'm on:
function edit($post_id = '')
{
//your code, form validation, etc...
//Prepare sidebar widget links
//Array key is link url, array value is link name
$widget['links'] = array (
'posts/single/' . $post_id => 'View post',
'posts/remove/' . $post_id => 'Remove post'
);
$this->sidebar('Options', $widget); //load sidebar
}
3. Finally the sidebar view displaying custom data passed from the controller:
<div id="sidebar">
<ul>
<li class="widget">
<div class="label"><?php echo $title; ?></div>
<ul>
<?php foreach ($links as $link => $value): ?>
<li><?php echo anchor($link, $value); ?></li>
<?php endforeach; ?>
</ul>
</li>
</ul>
</div>
Conclusion
Add the following code to any controller function for custom sidebar title and links:
$widget['links'] = array (
'controller/function' => 'Link Name 1',
'controller/function' => 'Link Name 2',
'controller/function' => 'Link Name 3'
);
$this->sidebar('Widget Title', $widget);
I think that gets it done. I'm not an expert in CodeIgniter, but I can tell you that's just fine. One thing you should always make sure is to validate the data passed to a function. In this case:
private function sidebar($title=FALSE, $widget=FALSE) {
if ($title && $widget) {
//process
}
else
return FALSE
}
}
Another way to do it is just to pass the links to the template (not the sidebar template, but your main template:
$data['sidebar'] = array('link/link'=>'My Link');
$this->load->view('mytemplate', $data);
And in the template you load the sidebar template and pass it the data:
<html>
<!--All my html-->
<?php $this->load->view('includes/sidebar', $data['sidebar']); ?>
</html>
This is just another option. But what you did is just fine.
I want to change the class names of the sidebar widgets on every different category page of WordPress and I figured the best way to do this would be to make a function in functions.php with all the conditions and return the required class name. I then called the function in the list tags of the register_sidebar function.
if (function_exists('register_sidebar')) {
register_sidebar(array(
'before_widget' => '<li class="sidebarModule">',
'after_widget' => '</li><!-- end module -->',
'before_title' => '<h2 class="moduleTitle "'.set_widget_title_color().'>',
'after_title' => '</h2>',
));
}
function set_widget_title_color() {
if(is_category('technology')) {
$color = "catNavColor1_active";
} elseif(is_category('gadgets')) {
$color = "catNavColor2_active";
} elseif(is_category('social-media')) {
$color = "catNavColor3_active";
} elseif(is_category('gaming')) {
$color = "catNavColor4_active";
} elseif(is_category('other')) {
$color = "catNavColor5_active";
}
return $color;
}
For some reason the above doesn't work. Please Help
Thanks
I think register_sidebars is called too early in the process, when the category is not defined yet. Have you tried implementing the dynamic_sidebar_params filter? I could not find anything in the WordPress Codex, but I found this nice example. Your other question on this topic also has a complete answer.
This only works if you implement the sidebars using dynamic_sidebar in your widget, as this function calls the dynamic_sidebar_params filter. If you have static widgets (defined in your template, not using the widget admin page), you should add a call to your function in that template code, like this:
<li class="sidebarModule">
<h2 class="moduleTitle <?php echo set_widget_title_color(); ?>">Widget title</h2>
<?php /* Your widget code */ ?>
</li><!-- end module -->