I have the following conditional tag, which a friend helped me with...

Currently it works doing..

1) If a featured image just show that.

2) If images attached show featured image wrapped in a link to the images attached..

Can there be a third?

3) If meta data just a text input field rw_post-vimeo just show that, its a Vimeo link..get_post_meta($post->ID, 'rw_post-vimeo',true)

I made an attempt below but it breaks it, is this possible?

<ul id="post-list">

<?php if (have_posts()) : while (have_posts()) : the_post(); ?>

$featured = get_post_meta($post->ID,'_thumbnail_id',true);
$attachments = get_children( 'post_type=attachment&orderby=menu_order&exclude='.$featured.'&post_mime_type=image&post_parent='.$post->ID );
 <li class="post">

 <span class="entry-published">
 <time datetime="<?php the_time('Y-m-d')?>"><?php the_time('F jS, Y') ?></time>

 <?php if ( !empty($attachments) ) : ?>
 <div class="entry-specific-content">

<a href="<?php echo get_bloginfo('template_directory').'/gallery.php?id='.$post->ID; ?>">

<?php if (has_post_thumbnail()): the_post_thumbnail();else: $first_img = wp_get_attachment_image_src($attachments[0]->ID, 'thumbnail-size');  ?>

<img src="<?php echo $first_img[0]; ?>" width="<?php echo $first_img[1]; ?>" height="<?php echo $first_img[2]; ?>" />
<?php endif; ?>
<div class="yes-gallery">
<a href="<?php echo get_bloginfo('template_directory').'/gallery.php?id='.$post->ID; ?>">See Rest Of Images</a>

<?php else: ?>
<div class="entry-specific-content">
<?php the_post_thumbnail(); ?>
<div class="no-gallery"></div>

<?php else: ?>
<div class="entry-specific-content">
<?php echo do_shortcode('[iframe_loader src="http://player.vimeo.com/video/'. get_post_meta($post->ID, 'rw_post-vimeo',true) .'?title=0&amp;byline=0&amp;portrait=0&amp;color=ffffff&autoplay=1" width="640" height="360" frameborder="0""]');?>

<div class="no-gallery"></div>

<?php endif; ?>

<h2 class="entry-title"><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2>

<div class="entry-content">
<?php the_content(); ?>


<?php endwhile; endif; ?>

Is it possible or no go, another way of doing it?

Thank you for anyones help :)

1 Answer 1


So, I would refactor this a bit:

$featured = ( has_post_thumbnail() ? get_post_thumbnail() : false );
$first_img = false;
$custom = false;

if ( ! $featured ) {
    // No featured image; look for attached images and get the first one
    $attachments = get_children( 'post_type=attachment&orderby=menu_order&post_mime_type=image&post_parent='.$post->ID ); 
    $first_img = ( $attachments ? wp_get_attachment_image_src($attachments[0]->ID, 'thumbnail-size') : false );
    // No attached images; look for custom-field image
    if ( ! $first_img ) {
        $custom_meta = ( get_post_custom($post->ID) ? get_post_custom($post->ID) : false );
        $custom = ( isset( $custom_meta['custom_field_name'][0] ) ? $custom['custom_field_name'][0] : "false" );

So, now, you can just step through each source; e.g.:

if ( $featured ) {
    // Post has a featured image; do something with it
} else if ( $first_img ) {
    // Post has attached image; do something with the first one
} else if ( $custom ) {
    // Post has image attached via custom field; do something with it
} else {
    // Otherwise, there's no featured image,
    // no attached images, and no image 
    // defined in post custom meta. Do something.

You just need to fit in your markup accordingly.

  • This is brilliant thank you. How about if its just text, no featured image, no image attached or video meta data, is that possible?
    – Jezthomp
    Commented Sep 24, 2011 at 22:10
  • Of course! You just need one more else. See updated answer. Commented Sep 24, 2011 at 23:48
  • I'll give that a go, thank you very much indeed :) (i'll probably break it)
    – Jezthomp
    Commented Sep 26, 2011 at 6:06

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.