WordPress.org

Ready to get started?Download WordPress

Forums

in_category erratic behavior (5 posts)

  1. kafuka
    Member
    Posted 8 months ago #

    Hi, i'm trying to load different stylesheets for different categories with this code in the head section, right after the line which loads the main style.css:

    <?php if( in_category (7) ) { ?> <link rel="stylesheet" href="<?php bloginfo('template_url')?>cat7.css" type="text/css" media="screen" /> <?php }
    elseif ( in_category (5) ) { ?> <link rel="stylesheet" href="<?php bloginfo('template_url')?>cat5.css" type="text/css" media="screen" /> <?php }
    elseif ( in_category (4) ) { ?> <link rel="stylesheet" href="<?php bloginfo('template_url')?>cat4.css" type="text/css" media="screen" /> <?php }
    elseif ( in_category (2) ) { ?> <link rel="stylesheet" href="<?php bloginfo('template_url')?>cat2.css" type="text/css" media="screen" /> <?php }
    else { ?> <?php } ?>

    this should be clean and pretty straightforward, but in reality the following happens:
    the css for cat5 loads when i'm viewing the category page for 5 (as intended) AND also when viewing categories 7 and 2.
    and the css for cat4 loads when I'm viewing the category page for 4 (as intended) AND the home/front page (latest posts).

    Why does this happening and how can I fix it? I could not find yet a solution even with using slugs or category names, or a little modified if statements...

  2. bcworkz
    Member
    Posted 8 months ago #

    Not sure if this will solve your problem, but you shouldn't directly output stylesheet meta links in a template head section. You should use wp_enqueue_style() to cause the correct meta link to appear. You may apply the same in_category() logic in your 'wp_enqueue_scripts' hook callback to enqueue the correct stylesheet. Hopefully this will solve your problem, but even if not, this is the correct approach anyway.

  3. ronangelo
    Member
    Posted 8 months ago #

    @kafuka
    in_category() checks if the current post is in a specific category. A single post may belong to 1 or more categories. A single post may belong to categories 1, 7 and 2. And also note that on the homepage there are a lot of latest posts on many different categories.

    If what you're going for is to style each post differently even on post pages(blog) then you can just use the post_class to style a post depending on the category. This way you won't need multiple stylesheet files.

  4. alchymyth
    The Sweeper & Moderator
    Posted 8 months ago #

    consider to use is_category() when you need to check for a category archive page;
    http://codex.wordpress.org/Function_Reference/is_category

    or check for single and in_category();
    if( is_single() && in_category() ) ....

  5. kafuka
    Member
    Posted 8 months ago #

    Hi,
    @bcworkz: you are right, that is a more elegant solution. i'm implementing it, thank you :)
    @ronangelo, alchymyth: thanks for the input, that will also help. in our case one post should only belong to one category (basically, we are doing workshops for children, and want to maintain separate categories to the 4 workshops with their distinct colourscheme, header img, and such so it will be easy to distinguish between them), but i guess there'll be posts with intersecting categories... in this case i'm going to look into how to implement your suggestions.
    Thanky you all, while i'm not completeley out of the water, you really helped:)

Reply

You must log in to post.

About this Topic