WordPress.org

Ready to get started?Download WordPress

Forums

How to define a class to category AND subcategory (18 posts)

  1. sottise
    Member
    Posted 1 year ago #

    Sorry in advance for my bad english :)
    I am searching how to define a class to a parent category AND the childs categories.
    This is the code I'm using for the parent category :

    <h1 <?php
    if (is_category('fruits')) {echo 'class="fruits"';}
    if (is_category('vegetables')) {echo 'class="vegetables"';}
    ?>><?php echo single_cat_title(); ?>
    </h1>

    This code works fine for the parent category, of course. But how to define it ALSO for the subcategories ?
    Thank you in advance !!

  2. cykeltomte
    Member
    Posted 1 year ago #

    I might be wrong, but something like this may work:

    if (is_category('fruits') || cat_is_ancestor_of( 'fruits' ));

    EDIT: nope not really, probably needs some modification.

    This page describes cat_is_ancestor_of(): http://codex.wordpress.org/Function_Reference/cat_is_ancestor_of

  3. sottise
    Member
    Posted 1 year ago #

    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. :)

  4. cykeltomte
    Member
    Posted 1 year ago #

    Do you mean the ID of the current subcategory? In that case maybe this will work: `

    $cat = get_query_var('cat');

    And just replace the ID with $cat.

  5. sottise
    Member
    Posted 1 year ago #

    The ID of the parent category actually.

    Where do I have to put your code ?

  6. cykeltomte
    Member
    Posted 1 year ago #

    What I posted above gives you the ID of the subcategory, and placing it anywhere above the "if" statement should be fine.

    I'm just guessing now, but this might give you the ID of the parent category:

    $q_cat = get_query_var('cat');
    $cat = get_category( $q_cat );
    $parent_cat = $cat->category_parent;

    If it works, just put $parent_cat where the parent ID should be.

  7. sottise
    Member
    Posted 1 year ago #

    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>
  8. sottise
    Member
    Posted 1 year ago #

    oh, wait, I try the new one

  9. sottise
    Member
    Posted 1 year ago #

    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>
  10. cykeltomte
    Member
    Posted 1 year ago #

    There's a ; missing after the_category( ' / ' ). Could be what's causing the error.

  11. sottise
    Member
    Posted 1 year ago #

    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>
  12. cykeltomte
    Member
    Posted 1 year ago #

    Try this:

    <?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"';}
    elseif ( in_category( 'vegetables' ) || post_is_in_descendant_category( $parent_cat ) ) {echo 'class="vegetables"';}
    ?>><?php the_category( ' / ' ); ?></h1>
  13. cykeltomte
    Member
    Posted 1 year ago #

    Otherwise, maybe something like this could work:

    <?php $cat = get_query_var('cat');
    $category = get_category($cat);
    $parent = $category->parent;
    
    if( $parent != 0 ) {
    $ID = $category->category_parent;
    $class = get_the_category($ID)->cat_name;
    }
    
    else $class = single_cat_title("", false);
    
    ?>
    <h1 class="<?php echo $class; ?>"><?php the_category('/'); ?></h1>
  14. sottise
    Member
    Posted 1 year ago #

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

  15. sottise
    Member
    Posted 1 year ago #

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

  16. cykeltomte
    Member
    Posted 1 year ago #

    Could you paste a link to your page.

  17. sottise
    Member
    Posted 1 year ago #

    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 ! :)

  18. cykeltomte
    Member
    Posted 1 year ago #

    Well, not sure if I really contributed with anything helpful, but I'm glad you solved it :)

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags

No tags yet.