• Hello!

    I am trying to use a conditional statement to test if a post is in certain categories. My archive page is set up as a list of thumbnails, and I want the loop to test if the posts have certain categories, and if they do, to display different elements (badges) on the thumbnail.

    I’ve been struggling to find a solution, I got as far as knowing that it wasn’t working because of the posts having multiple categories but I don’t know where to go from there.

    Just to give context (in case it’s not just the way I’m using the php) here is the loop on the archive page:

    <?php /* Start the Loop */ ?>
          <?php while ( have_posts() ) : the_post(); ?>
           // content-ventures is a single thumbnail to be displayed of the 'venture' post type
           <?php get_template_part( 'content', 'ventures' ); ?>
         <?php endwhile; ?>

    Here is my code on the content-ventures.php page. Any post can either have ‘sector-enterprise’ or ‘sector-consumer’ categories, but they could have BOTH the ‘gap-funded’ and ‘prototype-funded’ categories. So at most any post could have 3, and at the least the could just have 1 (their sector).

    <?php if ( in_category(array('gap-funded', 'sector-enterprise'))) {
    	        echo '<h2> Hello! </div>';
    	} elseif ( in_category(array('prototype-funded'))) {
    	        echo '<h2> Hi there! </div>';
    	} elseif ( in_category(array('prototype-funded', 'sector-enterprise'))) {
    		echo '<p>cats</p>';
    	} ?>

    Above I was just trying to get anything to appear, this isn’t all the configurations of outcomes, and something tells me there might even be a better way to do this. Looking for any advice!

    Thanks in advance!

  • The topic ‘Using conditional and in_category() to test if post is in category/categories’ is closed to new replies.