0

Currently I have the following loop:

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

<ul>
    <?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; ?>
            </li>   
        <?php else : ?>
            <li><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></li>   
        <?php endif; ?>
    <?php endforeach; ?>
    <?php wp_reset_query(); ?>
</ul>

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!

2 Answers 2

1

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; ?>
1
  • Perfect - I have gone with this method as it was easier for me to implement. Thanks again! Commented Mar 20, 2012 at 15:42
0

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:

<?php
$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() ) {
        $shows_sales->the_post();

        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.

2
  • 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 :) Commented Mar 20, 2012 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 :) Commented Mar 20, 2012 at 18:11

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.