WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] Conditional statement including category AND child-categories

[Resolved] Conditional statement including category AND child-categories

  • Nessdufrat

    @nessdufrat

    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 !

Viewing 15 replies - 1 through 15 (of 18 total)
  • Nessdufrat

    @nessdufrat

    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>
    Moderator Andrew Nevins

    @anevins

    Forum moderator

    The stupid question was the one not asked.

    Nessdufrat

    @nessdufrat

    ^^ 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 🙂

    Moderator Michael

    @alchymyth

    Forum Moderator

    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*/; } ?>
    Nessdufrat

    @nessdufrat

    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’)) ?

    Nessdufrat

    @nessdufrat

    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.

    Moderator Michael

    @alchymyth

    Forum Moderator

    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

    Nessdufrat

    @nessdufrat

    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

    Moderator Michael

    @alchymyth

    Forum Moderator

    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>
    Nessdufrat

    @nessdufrat

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

    Nessdufrat

    @nessdufrat

    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;
    }?>">
    Moderator Michael

    @alchymyth

    Forum Moderator

    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;
    }?>">
    Nessdufrat

    @nessdufrat

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

    Moderator Michael

    @alchymyth

    Forum Moderator

    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’

    Nessdufrat

    @nessdufrat

    Oh, thanks 🙂 That might come in handy too 🙂

Viewing 15 replies - 1 through 15 (of 18 total)
  • The topic ‘[Resolved] Conditional statement including category AND child-categories’ is closed to new replies.