0

I have the following news articles on the frontpage of a website I'm currently working on:

Screenshot

For this I'll need to write two simple PHP if statements, which I've already done successfully, but on the single.php it gets troublesome for me.

On the frontpage I have the following to display the two news articles in the screenshot:

<div id="articles">
<?php query_posts('cat=45&posts_per_page=1'); if (have_posts()) : while (have_posts()) : the_post(); ?> <!-- Query for "System Updates" -->
    <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='/wp-content/themes/bosted-system/_/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>
<?php endwhile; endif; ?>

<?php query_posts('cat=1&posts_per_page=1'); if (have_posts()) : while (have_posts()) : the_post(); ?> <!-- Query for "Information" -->
    <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='/wp-content/themes/bosted-system/_/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>
<?php endwhile; endif; ?>

Pretty straight forward. However, on the single.php-page I get a problem. I have the following:

<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='/wp-content/themes/bosted-system/_/inc/images/system-update-big.png'/>"; } else { echo "<img class='system-update' alt='Bosted Information' src='/wp-content/themes/bosted-system/_/inc/images/information-big.jpg'/>"; } ?></div>

It's the first time I've ever had to write a statement like this, and I really can't sort it out.

5
  • What doesn't work? What happens that you think should be different?
    – s_ha_dum
    Commented Jan 6, 2014 at 22:28
  • The last else (else { echo "<img class='system-update' alt='Bosted Information' src='./information-big.jpg'/>"; }) isn't showing on the single.php. Commented Jan 6, 2014 at 22:32
  • 2
    var_dump() your conditions and make sure they are what you expect them to be. Results might be wrong or your expectations might be wrong.
    – Rarst
    Commented Jan 6, 2014 at 22:37
  • Why hard code content into the template when you can hard code 2 widget areas inline and then add a featured post/page type widget so you can easily change the content. Commented Jan 6, 2014 at 22:46
  • 2
    In addition the the other comments, you should consider using WP_Query() instead of query_posts().
    – birgire
    Commented Jan 6, 2014 at 22:59

1 Answer 1

0

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...

3
  • Maybe the double quotes around the cat i.d in the conditional Commented Jan 7, 2014 at 1:42
  • I've learned quite a lot from your post. I'll give it a go! Commented Jan 9, 2014 at 20:36
  • @BradDalton: I actually din't try the code. If you have such experience, please edit the answer with the correction - you are welcome. :) Commented Jan 10, 2014 at 8:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.