0

I am using advanced custom fields pro to display an image if the option exists. If the option does not exist then it will look for a post thumbnail. I already have this code and it's working however I want to add an option to display a default image from my images folder if both options do not exist.

Here's the working code I currently have:

<?php if(get_field('photos')): ?>
<div class="img"><?php echo '<a class="image" href="' . get_permalink() . '"><div class="img"><img src="' . $model_pic[0]['sizes']['models'] . '" /></div><div class="info"><h3>' . get_the_title() . '</h3></div> </a> ' ?></div>

<?php else: ?>

<?php $small_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'models'); ?>
<div class="img"><?php echo '<a class="image" href="' . get_permalink() . '"><div class="img"><img src="' . $small_image_url[0] . '"/></div><div class="info"><h3>' . get_the_title() . '</h3></div> </a> ' ?></div>

<?php endif; ?>

I want to add a third else statement to the code so it will display an image from my theme images folder if there is no post thumbnail set.

Something like this: (but this does not work)

<?php if(get_field('photos')): ?>
<div class="img"><?php echo '<a class="image" href="' . get_permalink() . '"><div class="img"><img src="' . $model_pic[0]['sizes']['models'] . '" /></div><div class="info"><h3>' . get_the_title() . '</h3></div> </a> ' ?></div>

<?php elseif ?>

<?php $small_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'models'); ?>
<div class="img"><?php echo '<a class="image" href="' . get_permalink() . '"><div class="img"><img src="' . $small_image_url[0] . '"/></div><div class="info"><h3>' . get_the_title() . '</h3></div> </a> ' ?></div>

<?php else: ?>

echo '<img src="' . get_bloginfo( 'stylesheet_directory' ) . '/images/thumbnail-default.jpg" />';

<?php endif; ?>
2

The function you are looking for is has_post_thumbnail()

With this function you can check, if a featured image has been attached to a post and perform the following code:

<?php if( get_field( 'photos' ) ): ?>
    <div class="img">
        <?php echo '<a class="image" href="' . get_permalink() . '"><div class="img"><img src="' . $model_pic[0]['sizes']['models'] . '" /></div><div class="info"><h3>' . get_the_title() . '</h3></div> </a> ' ?>
    </div>
<?php elseif( has_post_thumbnail() ): 
    $small_image_url = wp_get_attachment_image_src( get_post_thumbnail_id(), 'models'); ?>
    <div class="img"><?php echo '<a class="image" href="' . get_permalink() . '"><div class="img"><img src="' . $small_image_url[0] . '"/></div><div class="info"><h3>' . get_the_title() . '</h3></div> </a> ' ?></div>
<?php else: ?>
    <div class="img">
        <img src="<?php echo get_template_directory_uri(); ?>/images/default.png" alt="" />
    </div>
<?php endif; ?>
  • 1
    @PaulCoppock if the answer solved your issue, don't forget to accept the answer by clicking the check mark on the left to it. :) – Mayeenul Islam Apr 12 '15 at 3:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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