• marcolang

    (@marcolang)


    Hi… how can I show posts from all category children?
    I’m trying “in_category(‘cat1’)” but it doensn’t show CAT1 children’s.

    Thanks

Viewing 3 replies - 1 through 3 (of 3 total)
  • Hi, I have the same problem.
    using widget logic with woocommerce, I manage to display a Vsubmenu on the corresponding category page, but not on the subcategories and product pages pertaining to this category.

    Thanks for any help.
    Cheers
    Tina

    Okay, these requests are a little older, but in case somebody comes looking for just this: I got equally frustrated today because WP doesn’t seem to have a condition for that, so I wrote one.

    This is custom-made for my site, so no guarantees. Also, because I only needed it in that context, the function expects $catarray to be an array. If you’re only looking for one category, either enter it as an array of one or add an extra condition (if (!is_array) or something) to the function. It’s all pretty bare-bones, too, no checking if the post actually has a chapter and if a function by that name already exists and all that. Feel free to use it as a starting point. Other than that, you should be able to just copy this to your function.php, then use “in_subcategory()” in Widget Logic:

    function in_subcategory ($catarray) {
    	$currentcatz = get_the_category( $post->ID ); 
    	foreach ($catarray as $catid) { 
    		foreach ($currentcatz as $currentcat) { 
    			if ( cat_is_ancestor_of( $catid, $currentcat->cat_ID ) )
    				{ return true; }
    			else 
    				{ return false; }
    			}
    		}
    	}

    … No wait, that was too simple.
    This should work, though:

    
    function in_subcategory ($catorcats) {
    	$truepositive = 0;
    	$currentcatz = get_the_category( $post->ID ); 
    	foreach ($currentcatz as $currentcat) {
    		if (is_array($catorcats)) { 
    			foreach ($catorcats as $arraycat) { 
    				if ( cat_is_ancestor_of( $arraycat, $currentcat->cat_ID ) )
    					{ $truepositive = 1; } 
    			}
    		} else { 
    			if ( cat_is_ancestor_of( $catorcats, $currentcat->cat_ID ) )
    				{ $truepositive = 1; } 
    		}
    	}
    	if ($truepositive == 1 ) 
    		{ return true; }
    	else 
    		{ return false; }
    }

    And now it also checks for arrays because once I had it, I started using it more and got fed up with using fake arrays.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Show all category children’ is closed to new replies.