sottise
Forum Replies Created
-
Forum: Fixing WordPress
In reply to: How to define a class to category AND subcategoryYEAY ! 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 ! ??Forum: Fixing WordPress
In reply to: How to define a class to category AND subcategorywell ! The fatal error is gone, but… It doesn’t recognize the class…
I am trying to figure it out by searching hereForum: Fixing WordPress
In reply to: How to define a class to category AND subcategorystill works for the first category fruits but not for the other one (vegetables)… Argh…
Forum: Fixing WordPress
In reply to: How to define a class to category AND subcategoryI’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>
Forum: Fixing WordPress
In reply to: How to define a class to category AND subcategoryHere 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>
Forum: Fixing WordPress
In reply to: How to define a class to category AND subcategoryoh, wait, I try the new one
Forum: Fixing WordPress
In reply to: How to define a class to category AND subcategoryYeay ! 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>
Forum: Fixing WordPress
In reply to: How to define a class to category AND subcategoryThe ID of the parent category actually.
Where do I have to put your code ?
Forum: Fixing WordPress
In reply to: How to define a class to category AND subcategoryThanks 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. ??