WordPress - How to get CSS classes of menu items with PHP? - php

I want to add an additional menu item to my navigation via my functions.php
The code below works, but how can I add the CSS classes which are used in the navigation to the li element automatically without writing each class manually in the below stated function= There must be something like get_classes_for_li_elements() - does someone know how I can achieve this?
Thanks!
add_filter( 'wp_nav_menu_home_items', 'add_itemcart_to_menu' , 10, 2 );
function add_itemcart_to_menu( $items, $args ) {
$menu_item_li = '<li>My Item</li>';
return $items . $menu_item_li;
}

I would write something like
$menu_item_li = 'My Item';
or asign a class used by your theme

Related

Remove class from my body tag. Wordpress. Genesis

I have this page here: http://staging.seedcreativeacademy.co.uk/course_type/digital-marketing/ and I'm trying to use a custom taxonomy.php age for it. (taxonomy-course_type.php).
The problem being I think it's taking styles and layout from a file called page-masonry.php inside my template.
I need this functionality to display the blog, but I want to display my Custom post type differently. So I think I need to remove the class 'masonry' from th body tag.
I've tried this code to do so as the first thing in my taxonomy-course_type.php page:
<?php
add_filter('body_class', 'remove_body_class', 20, 2);
function remove_body_class($wp_classes)
{
foreach($wp_classes as $key => $value)
{
if ($value == 'masonry') unset($wp_classes[$key]);
}
return $wp_classes;
}
genesis();
But it doesn't seem to do anything and my taxonomy-course_type.php doesn't seem to be being seen.
It's on the Genesis Framework, by the way, if that's any help.
Try Like This
add_filter( 'body_class', 'wpse15850_body_class', 10, 2 );
function wpse15850_body_class( $wp_classes, $extra_classes ) {
// List of the only WP generated classes allowed
$whitelist = array( 'portfolio', 'home', 'error404' );
// Filter the body classes
$wp_classes = array_intersect( $wp_classes, $whitelist );
// Add the extra classes back untouched
return array_merge( $wp_classes, (array) $extra_classes );
}

wordpress: set class of navigation element to content of element

i'm new to wordpress theming and I currently try to implement a navigation menu using icons. The framework i use requires me to set specific class names to achieve the icon being displayed - so what i basically want is:
<ul>
<li class="blog">blog</li>
....
So the class should be equal to the content
I found the following in the wordpress docs
add_filter('nav_menu_css_class' , 'special_nav_class' , 10 , 2);
function special_nav_class($classes, $item){
if(is_single() && $item->title == "Blog"){
$classes[] = "special-class";
}
return $classes;
}
This is how I currently display my menu
wp_nav_menu( array( 'theme_location' => 'primary', 'menu_id' => 'primary-menu' ) );
I'm kind of confused by the example given from the wordpress doc's since I cannot figure out the use of al the parameters or even where to place it.
I assume that you are registered your primary menu in functions.php.
After that you must put this code sample again in your functions.php.
What the code does is that it would launch the function special_nav_class for each menu item from primary-menu. As a parameter this function will receive the current menu item ( as post object ) and current classes applied for it as an array.
So, if you have menu with 5 elements, this function will run 5 times and each time it will receive current menu item and its classes array.
Basically something like this can do the job, if menu items are named properly:
function special_nav_class($classes, $item){
$classes[] = strtolower($item->title);
return $classes;
}

Make Menus in Wordpress use menu-item name instead of number for list item ID?

So after calling wp_nav_menu like:
<?php wp_nav_menu( array('menu' => 'Primary Menu' )); ?>
I end up with:
<li id="menu-item-1">
<li id="menu-item-2">
... ids like menu-item-1, menu-item-2, etc.
Is there some way to replace the numbers with the title of the page instead,so
menu-item-1 becomes menu-item-contact,
menu-item-2 becomes menu-item-store, etc?
You'll need to filter the ID using WordPress's nav_menu_item_id filter, which is applied in the nav-menu-template.php file. It takes three arguments, but the second one is what we need to actually get the title of the menu item, and turn it into something useful for the ID attribute. Here's a function to do that, which works for me in WordPress 3.4.1:
function custom_nav_menu_item_id($id, $item) {
$title = sanitize_title_with_dashes($item->title);
return 'menu-item-'.$title;
}
add_filter('nav_menu_item_id', 'custom_nav_menu_item_id', 10, 2);
This uses another WordPress function to sanitize the title into a string usable for the ID attribute (or any other HTML attribute).
Note that this function get's the menu item's title, which migth be different than the post or page title if you change it in the Menu panel from the admin.
Based on #Jeremy answer : I just add sanitize_title() to remove accents and special char.
function custom_nav_menu_item_id($id, $item) {
$title = sanitize_title_with_dashes(sanitize_title($item->title));
return 'menu-item-'.$title;
}
add_filter('nav_menu_item_id', 'custom_nav_menu_item_id', 10, 2);
/** for a better wp cleaning take a look # this gist and include it in your function.php */

Wordpress add_options_page

I would like to create two plugins for my wordpress blog and I need different admin settings for both.I am trying to adding plugin administration; but which selecting both settings together when select single.What's wrong with me.Here I am attaching what I done
add_action('admin_menu', 'sb_load_featured_slider');
function sb_load_featured_slider() {
$mypage = add_options_page('SB_Featured Slider', 'SB_Featured Slider', 8, array('sbslider','sbslider1','sbslider2'), 'sb_featured');
add_action( "admin_print_scripts-$mypage", 'sb_loadjs_admin_head' );
}
and my second plugin says as follows
add_action('admin_menu', 'sb_load_intro');
function sb_load_intro()
{
$mypage = add_options_page('SB Introduction', 'SB Introduction', 9, array('sbintro','sbjquery'), 'sb_intro');
add_action( "admin_print_scripts-$mypage", 'sb_introjs_admin_head' );
}
well I see one similarity between the 2 functions. You use the $mypage variable in both functions and use that for the second add action. try to use different variable names and see if it fix your problem.
eg: $mypage1, $mypage2

Add a custom class name to Wordpress body tag?

I'd like to place a directive in my theme's functions.php file which appends a classname to the wordpress body tag. Is there a built-in API method for this?
For example, my body tag code is...
<body <?php if(function_exists("body_class") && !is_404()){body_class();} else echo 'class="page default"'?>>
And it results in the following being written to the body tag (depending on the context in which the page is presented (page, post, logged-in, etc)
<body class="home blog logged-in">
Depending on the child theme I'm using at the time, I want it to be...
<body class="home blog logged-in mychildthemename">
You can use the body_class filter, like so:
function my_plugin_body_class($classes) {
$classes[] = 'foo';
return $classes;
}
add_filter('body_class', 'my_plugin_body_class');
Although, obviously, your theme needs to call the corresponding body_class function.
You can also use it directly inside the WP body_class function which will append the string inside body class.
eg.
$class="custom-class";
<body <?php body_class($class); ?>>
http://codex.wordpress.org/Function_Reference/body_class
To add more classes to the filter, just add another line that adds another value in to the array:
add_filter( 'body_class','my_body_classes' );
function my_body_classes( $classes ) {
$classes[] = 'class-name';
$classes[] = 'class-name-two';
return $classes;
}
In case you're trying to add classes to the body tag while in the Admin area, remember to use the admin_body_class hook instead. Note that it's a filter which works slightly different since it passes a string of classes rather than an array, so your code would look like this:
add_filter('admin_body_class', 'my_admin_body_class');
function my_admin_body_class($classes) {
return $classes . ' my_class';
}
Try this..
add_filter( 'body_class', 'custom_class' );
function custom_class( $classes ) {
if ( !is_front_page() ) {
$classes[] = 'example';
}
return $classes;
}
If you just want to add class based on your page template note that WP now automatically adds per-template class to body tag.
Simply edit your theme's header.php and change the class there (manually or according to some preset logic rules).

Categories