• Resolved foochuck

    (@foochuck)


    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!

Viewing 14 replies - 1 through 14 (of 14 total)
  • 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

    Thread Starter foochuck

    (@foochuck)

    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

    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

    Thread Starter foochuck

    (@foochuck)

    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’

    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

    Thread Starter foochuck

    (@foochuck)

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

    Thread Starter foochuck

    (@foochuck)

    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.

    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

    Thread Starter foochuck

    (@foochuck)

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

    Thread Starter foochuck

    (@foochuck)

    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.

    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.

    Thread Starter foochuck

    (@foochuck)

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

    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

    Thread Starter foochuck

    (@foochuck)

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

Viewing 14 replies - 1 through 14 (of 14 total)

The topic ‘Conditional for parent category’ is closed to new replies.