Forum Replies Created

Viewing 9 replies - 46 through 54 (of 54 total)
  • Thread Starter sottise

    (@sottise)

    YEAY ! Finally, it works !!

    I used the code mentioned here .

    <?php
    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;
    	}
    }
    ?>
    <h1 <?php if ( in_category( 'fruits' ) || post_is_in_descendant_category( ID ) ) {echo 'class="fruits"';}
    	  if ( in_category( 'vegetables' ) || post_is_in_descendant_category( ID ) ) {echo 'class="vegetables"';}
    	  	?>><?php the_category(' / '); ?></h1>

    Happy me ??
    Anyway, thank you so much for your help cykeltomte, I really appreciated it ! ??

    Thread Starter sottise

    (@sottise)

    well ! The fatal error is gone, but… It doesn’t recognize the class…
    I am trying to figure it out by searching here

    Thread Starter sottise

    (@sottise)

    still works for the first category fruits but not for the other one (vegetables)… Argh…

    Thread Starter sottise

    (@sottise)

    I’ve added the ; missing, but still have the same fatal error :/

    <?php $q_cat = get_query_var('cat');
    $cat = get_category( $q_cat );
    $parent_cat = $cat->category_parent; ?>
    	     <h1 <?php
    if ( in_category( 'fruits' ) || post_is_in_descendant_category( $parent_cat ) ) {echo 'class="fruits"';}
    if ( in_category( 'vegetables' ) || post_is_in_descendant_category( $parent_cat ) ) {echo 'class="vegetables"';}
    ?>><?php the_category( ' / ' ); ?></h1>

    The weird things, it’s that if I use only one if ( in_category, it works fine.

    Like :

    <?php $q_cat = get_query_var('cat');
    $cat = get_category( $q_cat );
    $parent_cat = $cat->category_parent; ?>
    	     <h1 <?php
    if ( in_category( 'fruits' ) || post_is_in_descendant_category( $parent_cat ) ) {echo 'class="fruits"';}
    ?>><?php the_category( ' / ' ); ?></h1>

    Thread Starter sottise

    (@sottise)

    Here is the code I’ve tried, but still have a fatal error :/

    <?php $q_cat = get_query_var('cat');
    	$cat = get_category( $q_cat );
    	$parent_cat = $cat->category_parent; ?>
    	                <h1 <?php if ( in_category( 'fruits' ) || post_is_in_descendant_category( $parent_cat ) ) {echo 'class="fruits"';}
    if (in_category( 'vegetables' ) || post_is_in_descendant_category( $parent_cat ) ) {echo 'class="vegetables"';}
    ?>><?php the_category( ' / ' ) ?></h1>
    Thread Starter sottise

    (@sottise)

    oh, wait, I try the new one

    Thread Starter sottise

    (@sottise)

    Yeay ! Thanks ! But I still have a problem…

    It works fine when I use it alone!
    But have a fatal error when I do this :

    <?php $cat = get_query_var('cat'); ?>
    	                <h1 <?php
    if ( in_category( 'fruits' ) || post_is_in_descendant_category( $cat ) ) {echo 'class="fruits"';}
    if ( in_category( 'vegetables' ) || post_is_in_descendant_category( $cat ) ) {echo 'class="vegetables"';}
     ?>><?php the_category( ' / ' ) ?></h1>
    Thread Starter sottise

    (@sottise)

    The ID of the parent category actually.

    Where do I have to put your code ?

    Thread Starter sottise

    (@sottise)

    Thanks cykeltomte !
    But yep, indeed, I have an error message with this code.

    Anyway, I’ve tried a new one and it’s works fine.
    I used in_category instead of is_category.

    if ( in_category( 'fruits' ) || post_is_in_descendant_category( 6 ) ) {echo 'class="fruits"';}

    Now, I am searching how to replace the category ID, to automatically add the subcategory without entering the category ID. ??

Viewing 9 replies - 46 through 54 (of 54 total)