0

First, let me start off by saying I'm very new to WordPress. I've only ever done basic themes before, this is my first big project.

I'm working on a design that has two sidebars on every page. Each sidebar displays the most recent post from two categories. These posts have Featured Images associated with them. I want to display the posts featured images in the sidebars on every page, but I can't figure out how to do it. The Feature Image always wants to pull the image from the current page, not the posts I'm trying to display. Or it just doesn't do anything at all.

Is there a way to do this, or do I have to resort to using a widget? I'd really prefer not to have to do a widget, because it'd be much easier for the client to just post something and have it appear in the sidebar.

Here's the code I'm trying to use (which currently just doesn't display any featured image):

<?php query_posts('category_name=Festivals','showposts=1'); ?>
<?php while (have_posts()) : the_post(); ?>
    <?php
        if(has_post_thumbnail( $id )) {
            echo get_the_post_thumbnail( $id, 'full');
        }                                                                                                                                                              
    ?>
    <p><?php the_time('M j') ?>: <a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></p>
<?php endwhile;?>
2

The $id variable isn't defined. Add this just before the conditional statement:

$id = get_the_ID();
  • 1
    Thanks! Seems to work perfectly :) I'll accept in 8 min when it'll let me. – JacobTheDev Jun 3 '13 at 15:17
0

First, don't use query_posts.

Second, neither has_post_thumbnail nor get_the_post_thumbnail need a post ID when inside a Loop, and in fact you don't really need get_the_post_thumbnail at all. You just need the_post_thumbnail. I'd suggest the following code instead:

if(has_post_thumbnail()) {
  the_post_thumbnail('full');
} 

And, if you aren't doing anything else beside echoing the featured image, you don't need the has_post_thumbnail check either. get_the_post_thumbnail, which the_post_thumbnail uses to retrieve the featured image markup will return an empty string if there is no featured image. All you really need is:

the_post_thumbnail('full');

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.