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.

How can I include this (or something similar)

<?php if ( get_post_meta($post->ID, 'url', true) ) { ?>

        <a href="<?php echo get_post_meta($post->ID, "url", $single = true); ?>"><?php the_post_thumbnail('');?></a>

<?php } else { ?>

        <?php the_post_thumbnail('');?>

<?php } ?>

Into this simple widget query?

query_posts('post_type=sponsors&meta_key=_vof_sponsor_level&meta_value=Gold');
if (have_posts()) : 
    echo "<div class='imageshadow sponsorwidgetslides'>";
while (have_posts()) : the_post();
    the_post_thumbnail('sponsorwidget');
endwhile;
    echo "</div><!--/.imageshadow-->";
echo "<a class='widgetlink' href='";
bloginfo('url');
echo "/sponsors'>View all Sponsors &raquo;</a>";
endif; 
wp_reset_query();

So basically, if the custom field "url" exists it outputs the featured image wrapped in an anchor tag, but if the custom field doesn't exist it just outputs the featured image.

Also, I know it's not ideal using query_posts, and that get_posts would be better, but I'm under orders here.

share|improve this question
    
Maybe a dumb question, but does the code above not work if put in your widget code? –  Shane Jan 24 '12 at 15:46
    
I tried it this morning but it didn't seem to. I'm not all that used to custom widgets, so having to echo the markup, etc. is a bit new to me –  Dean Elliott Jan 24 '12 at 15:48
    
Read somewhere that it could be $post->ID causing the problem (never playing with widgets so I don't know), but did you try using your code above in your widget, replacing $post->ID with get_the_ID() –  Shane Jan 24 '12 at 16:05

1 Answer 1

up vote 0 down vote accepted

I can't test it now, but the following should work...

    query_posts('post_type=sponsors&meta_key=_vof_sponsor_level&meta_value=Gold');
    if (have_posts()) : 
        echo "<div class='imageshadow sponsorwidgetslides'>";
        while (have_posts()) : the_post();
           if ( get_post_meta($post->ID, 'url', true) ) { ?>
             <a href="<?php echo get_post_meta($post->ID, 'url',true); ?>"><?php the_post_thumbnail('sponsorwidget');?></a>

          <?php } else {
              the_post_thumbnail('sponsorwidget');
            }
        endwhile;
        echo "</div><!--/.imageshadow-->";
        echo "<a class='widgetlink' href='";
        bloginfo('url');
       echo "/sponsors'>View all Sponsors &raquo;</a>";
   endif; 
wp_reset_query();

I've not come across any problems with $post->ID, but from what Shane has just commented, you could change that to get_the_ID()

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.