WordPress.org

Ready to get started?Download WordPress

Forums

Multiple category condtion tags (4 posts)

  1. Jay Hoffmann
    Member
    Posted 1 year ago #

    Hello,

    I am trying to serve posts from a few different categories on my homepage with different CSS styles. I have set-up the loop like this:

    <?php if (have_posts()) : while (have_posts()) : the_post();
      if( $post->ID == $do_not_duplicate ) continue; ?>
    						<?php if ( in_category('4') ) { ?>
               <article id="love" <?php post_class('clearfix'); ?> role="article">
               <?php } elseif ( in_category('5') ) { ?>
               <article id="jobs" <?php post_class('clearfix'); ?> role="article">
     <?php } else { ?>
               <article class="post">
     <?php } ?>

    But for some reason, the second id "jobs" is not being received. Even though a post is in category 5, it is assigning that post <article class="post">

    Am I missing something, is this coded wrong?

  2. Peter Baylies
    Member
    Posted 1 year ago #

    jhoffm34,

    By using quotes around the category id, you're making it a string; I think in_category is expecting an integer for the id. Try using, for example, in_category(4) instead of in_category('4') and see if that fixes your problem.

  3. Jay Hoffmann
    Member
    Posted 1 year ago #

    Still not working. And actually

    if( $post->ID == $do_not_duplicate ) continue; ?>
    						<?php if ( in_category('4') ) { ?>

    is working just fine.

    It is the second designation:

    <?php } elseif ( in_category('5') ) { ?>
               <article id="jobs" <?php post_class('clearfix'); ?> role="article">

    That is not working.

  4. Peter Baylies
    Member
    Posted 1 year ago #

    In that case, I don't know; your code looks pretty straightforward here; although you might potentially have issues if a post was in multiple categories.

Topic Closed

This topic has been closed to new replies.

About this Topic