0

I would like to add a custom class to my existing menu items. The problem is, that I would like to do something like this:

<li class="<?php echo get_post_meta($post->ID, "_icon", true); ?>">...</li>
<li class="<?php echo get_post_meta($post->ID, "_icon", true); ?>">...</li>
<li class="<?php echo get_post_meta($post->ID, "_icon", true); ?>">...</li>

or

<li><a class="<?php echo get_post_meta($post->ID, "_icon", true); ?>">...</a></li>

So I have a custom meta tags for posts and pages and they supposed to be a classes of li or a elements.

I know that I have to use custom walker, and I found this article, but I dont know how to add custom meta tags to $item_output and this example adds new menu.

Greetings

0

I've got the filter just for you: nav_menu_css_class

function wpse_175057_nav_menu_css_class( $classes, $item ) {
    if ( $item->type === 'post_type' && $class = get_post_meta( $item->object_id, '_icon', true ) )
        $classes[] = $class;
    return $classes;
}

add_filter( 'nav_menu_css_class', 'wpse_175057_nav_menu_css_class', 10, 2 );
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.