WordPress.org

Forums

[resolved] Conditional statement including category AND child-categories (19 posts)

  1. Nessdufrat
    Member
    Posted 2 years ago #

    Hi!
    I'm trying to do a conditional statement to assign a specific style to a category and all its child categories.

    <h1 class="page-title <?php
                    if (is_category('Édition')) :
    					echo 'edition';
    				elseif (is_category('Auto-publication')) :
    					echo 'autopublication';
     				else :
     					echo 'categorytitle';
    				endif;
    				?>">
    
    				<? echo single_cat_title( '', false ) ; ?></h1>

    Basically, I have a bunch of child categories for each category (the code is only to test it, I have 6 categories and about 6 subcategory for each category). The point would be for example to have if (is_category('Édition')) and "all of its child categories", blablabla...
    I'm looking for the code to call "all of its child categories". If I wanted a list or something, I could do it, but I'm not sure it would be ok here. Is there a way to say if "child category of Édition" ?

    Thanks !

  2. Nessdufrat
    Member
    Posted 2 years ago #

    Rahhh, stupid me, it was so simple...

    So, here it is, if somebody is looking for a solution :

    <h1 class="page-title <?php
                    if (is_category('Édition') || get_categories('child_of=4')) :
    					echo 'edition';
    				elseif (is_category('Auto-publication') || get_categories('child_of=3')) :
    					echo 'autopublication';
     				else :
     					echo 'categorytitle';
    				endif;
    				?>">
    
    				<? echo single_cat_title( '', false ) ; ?></h1>
  3. Andrew
    Forum moderator & snail smusher
    Posted 2 years ago #

    The stupid question was the one not asked.

  4. Nessdufrat
    Member
    Posted 2 years ago #

    ^^ My brain works in a stupid way, most of the time, when I ask a question, I find the answer by myself a few minutes later even if I had been looking for it for two hours. I guess trying to explain it to somebody else does the trick :)
    I might try to clean up the code a little, right now it's a bit ugly :)

  5. alchymyth
    Forum Moderator
    Posted 2 years ago #

    you could try to make a new function for the conditional check;

    example:

    function is_category_tree($branch='') {
    $cat = get_term_by('name',$branch,'category');
    if( !$cat ) $cat = get_term_by('slug',$branch,'category');
    if( !$cat ) $cat = get_term_by('id',(int)$branch,'category');
    $this_cat = get_query_var('cat');
    if( $cat && is_category() && ( $cat->term_id  == $this_cat || in_array( $cat->term_id, get_ancestors( $this_cat, 'category' ) ) ) )
      return true;
    else
      return false;
    }

    http://codex.wordpress.org/Function_Reference/get_term_by
    http://codex.wordpress.org/Function_Reference/get_ancestors
    http://php.net/manual/en/function.in-array.php

    can use either category name, category slug or category ID as argument.

    usage examples:

    <?php if( is_category_tree('Build It') ) { /*whatever*/; } ?>
    <?php if( is_category_tree('build-it') ) { /*whatever*/; } ?>
    <?php if( is_category_tree(157) ) { /*whatever*/; } ?>
  6. Nessdufrat
    Member
    Posted 2 years ago #

    Ok, thanks ! I might try that !
    I was thinking of keeping my conditional but making it cleaner using switch, like :

    <?php
    
    switch(id)
    
    {
    
    default:
          echo "whatever";
    break;
    
    case 4:
          echo "stuff for cat id 4";
    break;
    
    case 3:
          echo "stuff for cat id 3";
    break;
    
    }
    
    ?>

    I haven't been programming anything in php for 4 years, so I'm a bit rusty.

    What I could use for id could be your 'cat' variable? Or is there a more direct way to do it with my switch structure?

    Can I do something like :
    switch(is_category($id) || get_categories('child_of=$id')) ?

  7. Nessdufrat
    Member
    Posted 2 years ago #

    Ok, so no idea why, but yesterday it was working, and now, I get "édition" no matter what category it is...
    A friend has been working on the website too, adding categories, but she didn't touch the code or the existing category names. I double-checked the ID, they are still the same.
    The matter appears to be with get_categories('child_of=id'). When I get rid of this, it's fine. But I need to have the child categories having the same styling as the parent :(

    alchymyth, I tried your code, and it's conflicting with wordpress code, I think. I'm getting an error after the get_footer(); part, like unexpected $end or something.
    I checked all the brackets, they are correct.

  8. alchymyth
    Forum Moderator
    Posted 2 years ago #

    an error after the get_footer(); part, like unexpected $end or something

    it might help if you can post a link to your site;

    also if you could paste the full code of functions.php and of your template into a pastebin each - http://codex.wordpress.org/Forum_Welcome#Posting_Code

  9. Nessdufrat
    Member
    Posted 2 years ago #

    http://www.espacescomprises.com
    Here is my category template : http://pastebin.com/sHnQYj2V

    And the functions.php doc : http://pastebin.com/1iwknNGV

    (I tried first to put the function you wrote in my category template, but as it didn't work, I put it in functions.php. Doesn't work better, though)

    I can't really let the website with the error on it because I need to work on it and I can't do that if I don't see what I'm doing, so I'll leave it like that for 20 minutes, then I'll change it back to something that works.
    Just in case, here's the error : Parse error: syntax error, unexpected $end in /homez.490/espacesc/www/wp-content/themes/espacescomprises/category.php on line 41

  10. alchymyth
    Forum Moderator
    Posted 2 years ago #

    thanks for posting the link; you can reset it to whatever works for you.
    (ps: if you can still edit your post, you can remove the link if you like)

    this section in category.php:

    <h1 class="page-title <?php
                    if (is_category_tree(3)):
                                            echo 'edition';
                                    if (is_category_tree(4)) :
                                            echo 'autopublication';
                                    ?>">
    
                                    <? echo single_cat_title( '', false ) ; ?></h1>

    needs either 'endif's or needs to be rewritten to (colons removed):

    <h1 class="page-title <?php
                    if (is_category_tree(3)) echo 'edition';
                        if (is_category_tree(4)) echo 'autopublication';
                                    ?>">
    
                                    <? echo single_cat_title( '', false ) ; ?></h1>
  11. Nessdufrat
    Member
    Posted 2 years ago #

    Oh my... thanks. Does it show I've slept like 4 hours this night ? :/
    Thanks a lot !!!

  12. Nessdufrat
    Member
    Posted 2 years ago #

    Question, once more (sorry). I'm trying to clean up the code a little and I like switch statements.
    I've been trying to implement it, but it doesn't work, I'm not sure if I can do a switch on $branch, or if can simply call the function... I tried a bunch of solutions, but none worked :

    <h1 class="page-title <?php	
    
    		function is_category_tree($branch='') {
    $cat = get_term_by('name',$branch,'category');
    if( !$cat ) $cat = get_term_by('slug',$branch,'category');
    if( !$cat ) $cat = get_term_by('id',(int)$branch,'category');
    $this_cat = get_query_var('cat');
    if( $cat && is_category() && ( $cat->term_id  == $this_cat || in_array( $cat->term_id, get_ancestors( $this_cat, 'category' ) ) ) )
      return true;
    else
      return false;
    }		
    
    		switch (is_category_tree($branch)) {
        case 3:
            echo 'edition';
            break;
        case 4:
            echo 'autopublication';
            break;
        case 2:
            echo "";
            break;
    }?>">
  13. alchymyth
    Forum Moderator
    Posted 2 years ago #

    this might only work if the category you are checking is the top category - you could create a function to always return the top parent category of whatever category child archive you are viewing:

    <h1 class="page-title <?php
    $cat = get_query_var('cat');
    $top_cat = array_pop(array_merge(array($cat), get_ancestors($cat,'category')));	
    
    switch ($top_cat) {
        case 3:
            echo 'edition';
            break;
        case 4:
            echo 'autopublication';
            break;
        case 2:
            echo "";
            break;
    }?>">
  14. Nessdufrat
    Member
    Posted 2 years ago #

    Oh, yeah, that's exactly what I wanted right from the beginning ^^ Thanks a lot :)
    So, actually, what was different with the code you wrote (is_category_tree) ? Does the function allows other things than this piece of code doesn't?
    (you've been a huge help, BTW, thanks!!!)

  15. alchymyth
    Forum Moderator
    Posted 2 years ago #

    what was different with the code you wrote (is_category_tree) ?

    is_category_tree() would check for any position in a category hierarchy;
    example - say you have the hierarchy:

    'roots' -> 'trunk' -> 'branch' -> 'twig'

    and you are viewing the 'branch' category;
    then using is_category_tree('trunk') would return true, although 'trunk' is not the top category, but obviously 'branch' is within a lower hierachy of 'trunk'.

    the new code would always return (the category ID of) 'roots' (as this is the top parent category of the category archives category) regardless if you are viewing the 'branch'

  16. Nessdufrat
    Member
    Posted 2 years ago #

    Oh, thanks :) That might come in handy too :)

  17. Nessdufrat
    Member
    Posted 2 years ago #

    It's me again...
    I'm now trying to implement this in the single.php page. I'd like to have the name of the "lowest" category displayed at the top of the page (this is ok), but I'd like to also be able to check the top category in order to do my css switch.

    This here :

    <?php if ( have_posts() ) while ( have_posts() ) : the_post(); ?>
    
    <h1 class="page-title <?php
    $cat = get_query_var('cat');
    $top_cat = array_pop(array_merge(array($cat), get_ancestors($cat,'category')));	
    
    switch ($top_cat) {
        case 3:
            echo 'edition';
            break;
        case 4:
            echo 'autopublication';
            break;
        case 2:
            echo "";
            break;
    	default :
    	echo "pas de nom";
    }?>">

    Doesn't return anything, I'm getting a blank.

    I'm getting the category name like that :

    <?php
    $category = get_the_category();
    echo $category[0]->cat_name;
    ?>

    So I have my title, but I need to check the top category ID (not the sub-category, the one I'm getting the title from) and retrieve it.
    I have no idea why it's not working :(

  18. Nessdufrat
    Member
    Posted 2 years ago #

    And one more : I'm also trying to get the post div class to change according to the category it's in on the index page (main loop).
    The function doesn't work there either, it's returning a void.

    (basically, I always need to check for the top category ID and echo a class according to it, whether it's in the main loop or in the category loop or on the post page)

  19. Nessdufrat
    Member
    Posted 2 years ago #

    Ok, I did it :)

    <?php
    $category = get_the_category();
    $cat_tree = get_category_parents($category[0]->term_id, FALSE, ':', TRUE);
    $top_cat = split(':',$cat_tree);
    $parent = $top_cat[0];
    echo $parent;
    ?>

    I would have prefered the category ID (just because I couldn't get it to work and I'm frustrated), but finally, for what I'm doing, it's better to get the category name.

    I guess I'll soon have another problem, but right now, it's working :)

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags