when menu has specific class add submenu - php

Hello I want to make a menu in wordpress. When menu item has class 'category' then add to this item submenu. How I can do it? I tried like this, but it only add class.
add_filter('nav_menu_css_class' , 'my_special_nav_class' , 10 , 2);
function my_special_nav_class( $classes, $item )
{
if($item->title == 'kategorie' )
{
$classes[] ='special-class';
}
var_dump($item->title);
return $classes;
}

You need wordpress Menu Walker for this. See Example from here.
Demo Tutorial

Related

How can I add a custom classname to body on category page and all subcategories and posts of this page?

I just search for hours to find a solution for my problem. I need to add a body class to my category page and want to apply this class also on all subcategories and posts of the parent category.
For example i want category --> subcategory --> post to have the same class "orange" in body tag of HTML so i can style it properly.
Thanks for any advice.
Sounds like you're putting in too much effort to get a class 'orange' when you can use one of the classes automatically provided to you.
A typical wordpress body class looks something like this
single single-listing postid-3259 custom-header header-image content-sidebar agentpress-pro-blue windows chrome override
There may be more or less but there is usually a class you can use to target a specific page ('postid-3259') or a group of pages ('single-listing').
Without seeing the page(s) you want to target I can't get more specific. Once you have your body class selected you can select any element on the page by adding more selectors.
If you're dead-set on adding an orange class this link may help.
https://css-tricks.com/snippets/wordpress/add-category-name-body_class/
You could utilize the body_class filter and some conditional tags to do this.
add_filter( 'body_class', 'add_parent_category_to_body_class' );
function add_parent_category_to_body_class( $classes ) {
if ( !is_category() && !is_single() ) {
return $classes;
}
$categories = get_the_category();
foreach ($categories as $category)
if ($category->category_parent == 0 ) {
$classes[] = $category->slug;
} else {
$parent = get_category($category->category_parent);
$classes[] = $parent->slug;
}
return $classes;
}

How can I edit the Woocommerce product widget html class?

I would like to add a class to the existing woocommerce product widget.
The file '/includes/widgets/class-wc-widget-products.php' has the following filter.
echo apply_filters( 'woocommerce_before_widget_product_list', '<ul class="product_list_widget">' );
Is there a method that I can edit the html <ul class="product_list_widget"> in that filter within my theme?
this seems to be straight. Add the following in your functions.php
add_filter( "woocommerce_before_widget_product_list", "edit_product_widget_list", 1, 1 );
//$old_html contains -> <ul class="product_list_widget">
function edit_product_widget_list ( $old_html ) {
return '<ul class="my_own_class_here">'; // change your class here
}
Good luck :)

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;
}

WordPress - How to get CSS classes of menu items with 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

Wordpress theme - add text arrow if page has subpages

I want to show an arrow (add text) ">>" behind all the navigation items that have subitems/subpages. Is there a solution with add_filter or do i have to build the menu by myself?
Thanks for help!
Not exactly what you ask..
This adds a class="dropdown" to your menu's
// add a class 'dropdown' to menu items which have a sub menu
add_filter('nav_menu_css_class', 'check_for_submenu', 10, 2);
function check_for_submenu($classes, $item)
{
global $wpdb;
$has_children = $wpdb->get_var("SELECT COUNT(meta_id) FROM wp_postmeta WHERE meta_key='_menu_item_menu_item_parent' AND meta_value='" . $item->ID . "'");
if ($has_children > 0)
array_push($classes, 'dropdown'); // add the class dropdown to the current list
return $classes;
}

Categories