• Resolved pmontesinos

    (@pmontesinos)


    So, I’m trying to add_filter to the fdm_menu_item_classes filter.

    In addition to the classes already defined in array, I’d like to add one of my own. That class is dependent on whether or not there is content for that menu item.

    I added this to my functions.php file:

    function does_menu_item_have_content($classes, $this) {
    	$content = $this->content;
    	if (empty($content)) {
    		$classes[] = 'fdm-item-no-content';
    	}
    
        return $classes;
    }
    
    add_filter('fdm_menu_item_classes', 'does_menu_item_have_content', 11, 2);

    However, it does not work. I also passed by reference and that did not work either. Perhaps I’m missing something?

    https://wordpress.org/plugins/food-and-drink-menu/

Viewing 2 replies - 1 through 2 (of 2 total)
  • Hi pmontesinos,

    You were almost there! Here’s the correct code:

    function does_menu_item_have_content($classes, $item) {
    	if (empty($item->content)) {
    		$classes[] = 'fdm-item-no-content';
    	}
    
        return $classes;
    }
    
    add_filter('fdm_menu_item_classes', 'does_menu_item_have_content', 11, 2);

    In PHP, $this is a special variable. Within the class code that you read in Food and Drink Menu’s code, $this will be referring to the current object. But when it gets passed into a filter like this, you need to reference it in your function by some other name (any name that isn’t reserved by PHP).

    So in this case we accept the object as $item and look in $item->content for the content.

    Thread Starter pmontesinos

    (@pmontesinos)

    I knew I was close! Thank you for helping out a php noob!

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘fdm_menu_item_classes filter’ is closed to new replies.