Support » Plugin: If Menu - Visibility control for Menu Items » Conditionals for Category Is or Category Parent Is

  • I’m trying to set up two conditionals. First, would be to check if the page/post is in a particular category. The second instance would check to see if the page/post has an ancestor of a category.

    For the Category Is check I kinda have it in “if_menu_member_category”, but I just can’t figure out why I can’t set a $cat_to_check variable and pass it through the function call. I just wanted to set that variable before entering the function so that I could use it within the function. I have to comment out those lines.

    Second, I’m not really getting anywhere with the filter “if_menu_parent_category” to be able to check if the page/post category has an ancestor of the conditional category. I just don’t know enough about php.

    Third… it would be great if the plugin itself could be modified to include a drop-down picklist for Member Category and Parent Category so that it would be really easy just to pick a category, or parent category, to display/hide the menu on.

    Any help would be greatly appreciated.

    add_filter( 'if_menu_conditions', 'if_menu_member_category' );
    add_filter( 'if_menu_conditions', 'if_menu_parent_category' );
    $cat_to_check = '-Test';
    function if_menu_member_category( $conditions) {
      $conditions[] = array(
        'name'    => "Member Category is -Test", // name of the condition
        'condition' =>  function($category_to_check) {          // callback - must return TRUE or FALSE
          return in_category( "-Test" );
         //return in_category( $category_to_check );
      return $conditions;
    function if_menu_parent_category( $conditions ) {
      $category_to_check = get_term_by( 'name', '-Test', 'category' );
      $conditions[] = array(
        'name'    => "Parent Category is -Test", // name of the condition
        'condition' => function($item) {
           return post_is_in_descendant_category($category_to_check->term_id);
      return $conditions;
    /*if ( $category_to_check = get_term_by( 'name', '-Test', 'category' ))
     * Tests if any of a post's assigned categories are descendants of target categories
     * @param int|array $cats The target categories. Integer ID or array of integer IDs
     * @param int|object $_post The post. Omit to test the current post in the Loop or main query
     * @return bool True if at least 1 of the post's categories is a descendant of any of the target categories
     * @see get_term_by() You can get a category by name or slug, then pass ID to this function
     * @uses get_term_children() Passes $cats
     * @uses in_category() Passes $_post (can be empty)
     * @version 2.7
     * @link
    if ( ! function_exists( 'post_is_in_descendant_category' ) ) {
    	function post_is_in_descendant_category( $cats, $_post = null ) {
    		foreach ( (array) $cats as $cat ) {
    			// get_term_children() accepts integer ID only
    			$descendants = get_term_children( (int) $cat, 'category' );
    			if ( $descendants && in_category( $descendants, $_post ) )
    				return true;
    		return false;

  • The topic ‘Conditionals for Category Is or Category Parent Is’ is closed to new replies.