WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Currently I have the following loop:

$shows_sales = get_posts(array(
    'post_type' => 'show_sale',
    'post_status' => 'publish',
    'posts_per_page' => 3,
$done_image = false;

    <?php foreach ($shows_sales as $post) : setup_postdata($post) ?>
        <?php if (!$done_image && (has_post_thumbnail())) : ?>
            <li class="active">
                <a href="<?php the_permalink() ?>"><?php the_post_thumbnail('homepage'); ?></a>
                <?php $done_image = true; ?>
        <?php else : ?>
            <li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>   
        <?php endif; ?>
    <?php endforeach; ?>
    <?php wp_reset_query(); ?>

It will show one 'active' list item if post contains featured image, others just show the title in a normal list item.

What I need to add is a check that if none of the posts have a feature image, then insert a list item at the top of the unordered list.

I'm can't get my head around how to say 'if none have post thumbnail then show this else do the above'

Any help would be greatly appreciated!

share|improve this question
up vote 1 down vote accepted

Before you do your output, do something like this:

$no_thumbnails = true;
foreach( $shows_sales as $p ) {
    if( has_post_thumbnail( $p->ID ) )
        $no_thumbnails = false;

Then in your loop you can do something like this:

<?php if( $no_thumbnails === true ) : ?>
    <!-- Your output here -->
<?php endif; ?>
share|improve this answer
Perfect - I have gone with this method as it was easier for me to implement. Thanks again! – digitalclubb Mar 20 '12 at 15:42

If you need to add the item to the beginning of the list, then you'll need to assemble the HTML before echoing it. Something like this:

$shows_sales = new WP_Query(array(
    'post_type' => 'show_sale',
    'post_status' => 'publish',
    'posts_per_page' => 3
$found_post_thumbnail = false;

// Assemble the HTML for the list items first
$list_items = ''
if ( $shows_sales->have_posts() ) {
    while ( $shows_sales->have_posts() ) {

        if ( has_post_thumbnail() ) {
            $list_items .= '<li class="active"><a href="' . get_the_permalink() . '">' . get_the_post_thumbnail( 'homepage' ) . '</a></li>';
            $found_post_thumbnail = true;
        } else {
            $list_items .= '<li><a href="' . get_the_permalink() . '">' . get_the_title() . '</a></li>';

if ( !$found_post_thumbnail ) {
    $list_items = '<li>THIS IS MY EXTRA LIST ITEM</li>' . $list_items;

echo $list_items;

I switched this to a WP_Query request instead of get_posts() plus setup_postdata() - WP_Query is a bit more of a standardized (and nesting-safe) way to do it.

share|improve this answer
Thanks for this, I have an awful lot of content within my list items though, stripped it back to ask the initial question, so I panicked and went for the other option. Thanks though :) – digitalclubb Mar 20 '12 at 15:43
OK. The downside of the other option is that you end up looping through the items twice, which - depending on the number of items involved - could mean a hit on efficiency. But the most important thing is that you've got it working, optimized or not :) – Boone Gorges Mar 20 '12 at 18:11

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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