Forums

Changing style if post is in category 1 AND category 2 (9 posts)

  1. dmx09
    Member
    Posted 1 year ago #

    Hi all, I wondered if anyone can shed some light on a small issue I'm having.

    I've got an events page (which just loads posts in 3 different events categories).

    I'm using some conditional statements to control some small graphics that display based on what type of event it is. My questions is, can I use 'in_category' to do something only if the particular post is in 2 categories?

    For example:

    <?php
    	if ( in_category( 'new events' AND 'old events' )) {?>

    So if the post is in the 'New Events' AND 'Old Events' cats, then do something...

    Any thoughts most appreciated!

  2. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    <?php
    	if ( in_category( 'new events' ) && in_category( 'old events' ) ) {
    ?>

  3. dmx09
    Member
    Posted 1 year ago #

    Hi and many thanks,

    I've realised that maybe I'm approaching this incorrectly but not quite sure! This is a rough of what I have, not quite working. I presumed that if I could stipulate the style for posts that were in 2 categories, then it would over-ride the style for posts in single cats - this doesn't seem to be the case.

    [Code moderated as per the Forum Rules. Please use the pastebin]

    Might have a re-think!

  4. dmx09
    Member
    Posted 1 year ago #

  5. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    might be a problem with the understanding the if/elseif/else structure:
    http://php.net/manual/en/control-structures.elseif.php

    even if the post is in both categories, the first if statement will be true and the else (with the &&) will not get checked.

    try:
    http://pastebin.com/tm1f9gsC

    depending on the location of that code, you also might need to use the absolute paths to the images

  6. dmx09
    Member
    Posted 1 year ago #

    thanks alchymyth, will give it a go! ;-)

  7. dmx09
    Member
    Posted 1 year ago #

    As it happened I finally didn't need to combine styles but thanks for your help! I have another question if anyone has a spare moment!

    I want to give posts different colours depending on which category they are in. I've tried this:

    http://pastebin.com/EVhza7mM

    But I get an error (site doesn't load at all). My php is not good so I'm probably doing something silly here? :-)

  8. alchymyth
    The Sweeper & Moderator
    Posted 1 year ago #

    remove line 4, 11, and 18; and change (line 23) the <?php endif; ?> to <?php } ?>

  9. dmx09
    Member
    Posted 1 year ago #

    Funnily enough I had tried that earlier this morning but I get the same page error. Here's my entire page, just in case there is something I missed & if you have time.. many thanks again.

    http://pastebin.com/S5gwjmTm

Topic Closed

This topic has been closed to new replies.

About this Topic