Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm creating the search page for my wordpress site. The problem I'm having is that most of my articles have custom post types that display a featured image. The problem is that some do not. I am trying to figure out how I can tell my loop to include the custom post type if it exists but to ignore that section of code if it does not exist. Here's my code:

    <div id="wrapper-article" class="grid_12">

    <?php 
        if(function_exists('wp_paginate')) {
            wp_paginate();
        } 
    ?>

<?php if (have_posts()) : ?>

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

        <div <?php post_class() ?> id="archives">

            <h4><a href="<?php the_permalink() ?>"><?php the_title(); ?></a></h4>

            // HERE'S THE STUFF THAT IS OPTIONAL

                <a href="<?php the_permalink(); ?>"><img src="<?php bloginfo(url); ?><?php echo get_post_meta($post->ID, 'featured-image-large', true); ?>" title="<?php echo get_post_meta($post->ID, 'featured-image-title', true); ?>" /></a>

            // END OPTIONAL STUFF

                <?php the_excerpt(); ?>
                <a href="<?php the_permalink(); ?>" class="button-blue"><span>Continue Reading</span></a>

        </div>

    <?php endwhile; ?>

    <?php 
        if(function_exists('wp_paginate')) {
            wp_paginate();
        } 
    ?>

<?php else : ?>

    <h2>No posts found.</h2>

<?php endif; ?>

</div><!-- end div#wrapper-article -->

You can see the test site search results here: http://testserver1.staceylanehosting.net/?s=jesus

The first two results are pages that do not have images associated with them. The rest are posts. But there are posts that do not have images as well and I would like the if else statement to work for those too.

EDIT: @andrewkthx: Your answer wasn't exactly correct but it did lead me to find the correct answer so thank you for posting. :)

The correct method is:

<?php if ( get_post_meta($post->ID, 'featured-image-large', true) ) : ?>

    <a href="<?php the_permalink(); ?>"><img src="<?php bloginfo(url); ?><?php echo get_post_meta($post->ID, 'featured-image-large', true); ?>" title="<?php echo get_post_meta($post->ID, 'featured-image-title', true); ?>" /></a>

<?php endif; ?>
share|improve this question

1 Answer 1

up vote 0 down vote accepted

No need to be that complex, you can use the has_post_thumbnail function

    <?php if(has_post_thumbnail()): ?>
      <a href="<?php the_permalink(); ?>"><img src="<?php bloginfo(url); ?><?php echo get_post_meta($post->ID, 'featured-image-large', true); ?>" title="<?php echo get_post_meta($post->ID, 'featured-image-title', true); ?>" /></a>

   <?php endif; ?>

et voila! =)

share|improve this answer

Your Answer

 
discard

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.