WordPress.org

Ready to get started?Download WordPress

Forums

Custom Menu Classes (2 posts)

  1. imrandyk
    Member
    Posted 2 years ago #

    I'm trying to add a custom class to the menus that are generated. I am adding a custom field to each page/post and I need the value to be in the class. Here is my code.

    add_filter('nav_menu_css_class' , 'special_nav_class' , 10 , 2);
    	function special_nav_class($classes, $item){
    		$color = get_post_custom($item->object_id, 'color', TRUE);
    		echo "[".$item->object_id."][".$color."]";
    		if($color == "green"){
    			$classes[] = 'green';
    		}else if($color == "yellow"){
    			$classes[] = 'yellow';
    		}else{
    			$classes[] = 'pink';
    		}
    		return $classes;
    	}

    The part the does the echo is spitting out the correct ID but the $color is always an array. I can't figure out what is going wrong.

  2. Big Bagel
    Member
    Posted 2 years ago #

    get_post_custom only takes one argument and always returns an array.

    Function Reference/get post custom

    I think you're looking for get_post_meta:

    $color = get_post_meta( $item->object_id, 'color', true );

    Function Reference/get post meta

Topic Closed

This topic has been closed to new replies.

About this Topic