Forums

[resolved] Conditional for parent category (15 posts)

  1. foochuck
    Member
    Posted 11 months ago #

    I have a parent category:

    News

    And under that categories I have sub categories

    Sports, Featured, & Breaking

    I'm using is_category('news'); to check whether the current page is the News, however if the user lands on one of the posts from the children categories sports, featured & breaking, I'd like to check to see if the parent is News with an if statement.

    I have looked through the Codex and have not been able to find the code to accomplish this. Any suggestions?

    Thanks!

  2. esmi
    Theme Diva & Forum Moderator
    Posted 11 months ago #

    The obvious solution would be to use:

    <?php if ( in_category( array( 'Sports', 'Featured', 'Breaking' ) )):?>
    [do stuff]
    <?php endif;?>

    http://codex.wordpress.org/Template_Tags/in_category

  3. foochuck
    Member
    Posted 11 months ago #

    esmi,

    Is there a dynamic solution? This list of sub categories is going to continue to grow and I'd like to set it up so it's a dynamic reference to the parent (news) category.

    -foo

  4. esmi
    Theme Diva & Forum Moderator
    Posted 11 months ago #

    You could write a conditional based on get_ancestors.

    <?php
    // Assumes the id of the News category is 20
    $in_news = 0 // set a flag varable to false
    $cats = get_the_category();
    foreach ( $cats as $cat ) {
    	if( in_array( '20', get_ancestors( $cat, 'category' ) ) ) $in_news = 1; // set flag to true
    }
    if( $in_news == 1) :
    [ do stuff ]
    else :
    [ do other stuff ]
    endif;?>

    http://codex.wordpress.org/Function_Reference/get_the_category
    http://codex.wordpress.org/Function_Reference/get_ancestors

  5. foochuck
    Member
    Posted 11 months ago #

    esmi,

    This is the code I'm using, however it's not working correctly:

    <h1 id="logo">
    <?php
    $in_news = 0; // set a flag varable to false
    $cats = get_the_category();
    foreach ( $cats as $cat ) {
    	if( in_array( '23', get_ancestors( $cat, 'category' ) ) ) $in_news = 1; // set flag to true
    }
    if( $in_news == 1 || is_category(23)) : ?>
    <a href="http://localhost/test"><img width="345" height="176" src="http://localhost/test/wp-content/uploads/logo-old.png"></a>
    <?php else : ?>
    <a href="http://localhost/test"><img width="345" height="176" src="http://localhost/test/wp-content/uploads/logo.png"></a>
    <?php endif; ?>
    </h1>

    The else works on all pages. 'is_category(23)' works for the news page. However for all news sub categories, it's showing logo.png instead of 'logo-old.png'

  6. alchymyth
    The Sweeper
    Posted 11 months ago #

    are you using this in a category archive psge?

    the get_the_category(); part is based on the assumption that this is in a single post template (single.php).

    for a category archive page, try to rewrite the code to:

    <h1 id="logo">
    <?php
    $in_news = 0; // set a flag varable to false
    if( in_array( '23', get_ancestors( get_query_var('cat'), 'category' ) ) ) $in_news = 1; // set flag to true
    
    if( $in_news == 1 || is_category(23)) : ?>
    <a href="http://localhost/test"><img width="345" height="176" src="http://localhost/test/wp-content/uploads/logo-old.png"></a>
    <?php else : ?>
    <a href="http://localhost/test"><img width="345" height="176" src="http://localhost/test/wp-content/uploads/logo.png"></a>
    <?php endif; ?>
    </h1>

    n

  7. foochuck
    Member
    Posted 11 months ago #

    I'm using this in header.php so it will be global throughout all pages on my site.

  8. foochuck
    Member
    Posted 11 months ago #

    I should clarify, I'm using this in header.php but I need it to apply to single post pages, where you are seeing the permalink for a blog post.

    I need it to detect if the article has the parent news category.

  9. alchymyth
    The Sweeper
    Posted 11 months ago #

    I'm using this in header.php so it will be global throughout all pages on my site.

    however, the only two scenarios, where this makes sense, is a single post, or a category archive;

    possibly you need to combine both codes:

    example:
    http://pastebin.com/BAXjA1jj

  10. foochuck
    Member
    Posted 11 months ago #

    That code works for the category pages, but not for the single post pages.

  11. foochuck
    Member
    Posted 11 months ago #

    PS - I did change the 20 to 23 in your code for single post as that's the correct ID for my news category, however it's still not working.

  12. alchymyth
    The Sweeper
    Posted 11 months ago #

    as i copied the code of @esmi's reply, i might hav ethe wrong cat id in this line (line 8):

    if( in_array( '20', get_ancestors( $cat, 'category' ) ) ) $in_news = 1; // set flag to true

    please have a look and correct it if neccessary.

  13. foochuck
    Member
    Posted 11 months ago #

    Yes, I did catch that the cat id was off. No luck on the single post pages yet...

  14. alchymyth
    The Sweeper
    Posted 11 months ago #

    my bad - i missed to use the category ids from the single post for the comparison:
    (in line 8)
    if( in_array( '23', get_ancestors( $cat->term_id, 'category' ) ) ) $in_news = 1; // set flag to true

    new pastebin: http://pastebin.com/Y7fS27c6

  15. foochuck
    Member
    Posted 11 months ago #

    I was just printing out the array and saw that too...thanks very much!

Reply

You must log in to post.

About this Topic