Post Thumbnail on Single (if elseif else)

I tried to refactor the code in a preferred way, that’s all. Logically, it seems to work. What I did is:

  • I preferred WP_Query() for such custom query, because experienced WordPress enthusiasts like it that way (We can learn more)
  • I changed the hard-coding into WordPress way to get the theme directory, using get_template_directory_uri()
  • And finally, I used wp_reset_postdata() to reset the query what we started with WP_Query() in that block

So here is the new code:

<div id="articles">
<?php $system_updates = new WP_Query( array('cat'=>'45', 'posts_per_page'=>'1') ); //Query for "System Updates" ?>
<?php if ($system_updates->have_posts()) : while ($system_updates->have_posts()) : $system_updates->the_post(); ?>
    <div class="unit one-of-three system-update">
        <article <?php post_class() ?> id="post-<?php the_ID(); ?>">

            <?php if ( has_post_thumbnail() ) { the_post_thumbnail(); } else { echo "<img src="" . get_template_directory_uri() . "/_/inc/images/system-update-small.png" alt="Bosted System Update">"; } ?>

            <div class="description">
                <span class="date">System Update</span>
                <a href="<?php the_permalink() ?>"><h2><?php the_title(); ?></h2></a>
                <span class="date"><?php the_time('F j, Y') ?></span>
            </div>

        </article>
    </div> <!-- .system-update -->
<?php endwhile; endif; wp_reset_postdata(); ?>

<?php $information = new WP_Query( array('cat'=>'1', 'posts_per_page'=>'1') ); //Query for "Information" ?>
<?php if ($information->have_posts()) : while ($information->have_posts()) : $information->the_post(); ?>
    <div class="unit one-of-three info">

        <article <?php post_class() ?> id="post-<?php the_ID(); ?>">

            <?php if ( has_post_thumbnail() ) { the_post_thumbnail(); } else { echo "<img src="" . get_template_directory_uri() . "/_/inc/images/information-small.jpg" alt="Bosted System Information">"; } ?>

            <div class="description">
                <span class="date">Information</span>
                <a href="<?php the_permalink() ?>"><h2><?php the_title(); ?></h2></a>
                <span class="date"><?php the_time('F j, Y') ?></span>
            </div>

        </article>
    </div> <!-- .info -->
<?php endwhile; endif; wp_reset_postdata(); ?>
</div> <!-- #articles -->

And about the single.php, I refactored the code into the same way. Just make sure that, you have a the_post() at the very beginning:

<?php the_post(); ?>
<div class="banner"><?php if ( in_category('1') ) { the_post_thumbnail(); } elseif ( in_category('45') ) { echo "<img alt="Bosted System Update" class="system-update" src="" . get_template_directory_uri() . "/_/inc/images/system-update-big.png"/>"; } else { echo "<img class="system-update" alt="Bosted Information" src="" . get_template_directory_uri() . "/_/inc/images/information-big.jpg"/>"; } ?></div>

Let’s just try with these. I don’t know why your code is not working…