• I’ve developed an alternate post.php template that I use for posts in a certain category. But what I’d like to do now is modify the conditional a little, and PHPwise I’m a little lost.

    This is how my code is now:

    <?php if ( in_category('26') ) { ?>
            <div class="post-ws">
            <?php require('post-ws.php'); ?>
    <?php } else { ?>
            <div class="post">
            <?php require('post.php'); ?>
    <?php } ?>

    How would I change the IF to use the alternate post-ws.php if a post is in category 26 and not in any other category aside from category 3?

    i.e.:
    Post is in…………………………….Template to use
    Cat-26 only…………………………post-ws.php
    Cat-26 & Cat-3……………………post-ws.php
    Cat-26 & Cat-Other…………….post.php
    Cat-26, Cat-3 & Cat-Other…..post.php

Viewing 9 replies - 1 through 9 (of 9 total)
  • I just saw a similar question about Pages:
    https://www.ads-software.com/support/topic/162289?replies=4#post-710615
    I am not a coder by any means but I think the logic should be similar.

    this covers the php part of your question fairly well.

    https://mgeisler.net/php-tutorial/control-structures/

    specifically the bits that go on about this sort of stuff:

    To compare the value of a variable up against other variables or numbers, you have a number of different comparison-operators:

    * ==: Is equal to,
    * !=: Is not equal to,
    * <: Is strictly less than,
    * <=: Is less than or equal to,
    * >: Is strictly greater than,
    * >=: Is greater than or equal to,
    * && (and): Both left and right arguments must be true,
    * ||, or: Left or right argument must be true (or both),
    * ^, xor: Exactly one of the left and right arguments must be true.
    * !: Negation, changes true to false and false to true.

    Using these operators you can construct every Boolean expression you’ll ever need. Take for example the following if-statement:

    if (($x == 0 && $y != 0) || ($x != 0 && $y == 0))
      // Exactly one of $x and $y is zero.
    }
    Thread Starter dcroe05

    (@dcroe05)

    rudolf45,

    Thanks for the link. But what that doesn’t answer is the part I’m having the most trouble with. How do I phrase an if condition to result in true if a post falls into a category other than 26 & 3?

    grrrr.

    if ($cat != 26 && $cat != 3)

    please read.

    Thread Starter dcroe05

    (@dcroe05)

    whooami,

    I could have sworn I tried that.

    Thank you for your time. I’ll get back to this in the morning with fresh eyes.

    if (!is_category(’26’) && !is_category(‘3’) )

    Didnt I just explain the logic?

    Thread Starter dcroe05

    (@dcroe05)

    Yes you did explain it. And I said thank you.

    However, neither

    if ($cat != 26 && $cat != 3)
    or
    if (!is_category('26') && !is_category('3') )
    worked.

    When presented with a category that is part of categories 26, 3 & 1, each of these statements evaluated as true. Likewise, they evaluated as true for a post in categories 26 & 3.

    Nevermind…I’m going to try a different solution.

    if ($cat != 26 && $cat != 3)

    thats not going to work because I made up the variable $cat for the sake of the explanation ??

    Its easier to type.

    is_category checks to see if a particular category page is being displayed.

    in_category checks to see if a given post is in a category.

    you need this:

    if (!in_category('26') && !in_category('3') )

    Thread Starter dcroe05

    (@dcroe05)

    That one doesn’t work either I tried that before I even posted this topic.

    Thank you for your help. I’m going to try to solve the problem a different way.

Viewing 9 replies - 1 through 9 (of 9 total)
  • The topic ‘Syntax question for multiple is_category’ is closed to new replies.