Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

*CLARIFICATION: I'm trying to style the single post view of some categories & sub categories. The suggested solution works, but only on a page of posts from the selected categories.

Eg. If a post has a category 0f 59 (or is a child category of 59) then load a stylesheet.

Here's what I've tried from the Codex-

<?php 
// if the category is 59 or a 59 SUBcategory 
if (cat_is_ancestor_of(59, $cat) or in_category(59)): ?>
<link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/products.css" type="text/css" media="screen" /><?php endif; ?>

Any ideas why it's not working? Thanks

share|improve this question

1 Answer 1

up vote 3 down vote accepted

Try this if statement instead:

if ( is_category() && ( is_category( 59 ) || cat_is_ancestor_of( 59, get_queried_object_id() ) ) )

Update: To work for single posts:

if ( is_single() && $terms = get_the_category( get_queried_object_id() ) ) {
    foreach ( $terms as $term ) {
        if ( $term->term_id == 59 || cat_is_ancestor_of( 59, $term->term_id ) ) : ?>            
            <link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/products.css" type="text/css" media="screen" />
        <?php
            break;
        endif;
    }
}
share|improve this answer
    
Thanks DM, but think I have a syntax prob. Used this: 'code' <?php if ( is_category() && ( is_category( 59 ) || cat_is_ancestor_of( 59, get_queried_object_id() ) ) ): ?> <link rel="stylesheet" href="<?php bloginfo('template_url'); ?>/products.css" type="text/css" media="screen" />?><?php endif; ?> '/code' –  speedypancake Nov 7 '12 at 12:55
    
Anybody have any hints on how to use the "get_queried_object_id()" bit to add my stylesheet??? Thanks –  speedypancake Nov 7 '12 at 14:06
    
@user22537, you have an extra ?> just before <?php endif, otherwise the code looks fine. What is the error? –  s_ha_dum Nov 7 '12 at 15:14
    
Hey s_ha_dum - Thanks for pointing that out. No particular error, just not using the stylesheet I wanted. –  speedypancake Nov 7 '12 at 15:18
    
Just figured-out what was going on. The suggested solution DOES work for displaying a page list of sub categories. But what I was trying to achieve though was to apply the alternative style to the SINGLE post view of posts in a sub category. Apologies if the question was confusing - I'll have a look at re-writing if necessary. –  speedypancake Nov 7 '12 at 16:44

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.