WordPress.org

Ready to get started?Download WordPress

Forums

in_category: with multiple categories (4 posts)

  1. carlosbronze
    Member
    Posted 6 years ago #

    Hey all!

    Ive been searching the topics with in_category questions to solve a little problem im encountering but i cant seem to find a solution.

    Im using in_category() functions to change the layout of my site for some posts.
    I have a "blog", "articles" and "classes" pages. When you see the "blog" you get all sorts of posts, whereas on "articles" you see posts that only appear on the "articles" page.
    So far so good.

    My problem is on the "classes" page, when listing the posts i get a specific color for my header an links, but once you click on a specific post it shows the "blog" color.

    My guess is that, because those posts belong to more than one category (the "articles" posts have only the articles cat), its messing up with the color-choosing function.

    In short: When using in_category(), if the post belongs to more than one category, the function doesnt help.

    Is there a way to bypass this problem?

    Thank you for your time.

    ps: this is my function (cleaned up) and uses...

    function getColorScheme() {
    if (is_Page('articles') || in_category(4)) { $color = "articles"; }
    elseif (is_Page('classes') || in_category(23)) { $color = "classes"; }
    else { $color = "blog"; }
    echo $color;
    }

    i put it as a class on my <body>
    <body class="<?php echo getColorScheme(); ?>" >

    so it changes my CSS
    .blog A { color: #783e6e; }
    .articles A { color: #842222; }

  2. aradams
    Member
    Posted 6 years ago #

    I would like to do this too. I also noticed that in_category doesn't work for multiple categories.

    I tried:

    <?php
     $post = $wp_query->post;
     if ( in_category(14) && in_category(3) ) {
     include(TEMPLATEPATH . '/single_current.php');
     } else {
     include(TEMPLATEPATH . '/single_back.php');
     }
     ?>

    WordPress ignores the AND operator here.

  3. Czymra
    Member
    Posted 5 years ago #

    Oh man, hasn't there been a solution yet? I'm making a magazine type website and even though I use template files for category-7.php or so, whenever I assign a post to multiple categories, it only comes up in one of them.

  4. Umbrovskis.com
    Member
    Posted 5 years ago #

    instead of AND (&&) use OR (||)
    it works for me on WP 2.6.2

    <?php
     $post = $wp_query->post;
     if ( in_category(14) || in_category(3) ) {
     include(TEMPLATEPATH . '/single_current.php');
     } else {
     include(TEMPLATEPATH . '/single_back.php');
     }
     ?>

Topic Closed

This topic has been closed to new replies.

About this Topic