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 currently having some difficulties showing a featured image for the first post only.

Currently the image only shows if the post is the only post or if the post is a sticky post.

(I realize this isn't the preferred code for displaying a featured image, but it was the only code I could come up with having a html5 template)

AFAIK it shouldn't interfere with the code in question though.

I hope someone could perhaps assist.

This is the code I currently have:

<?php $count = 1; ?>
<?php while (have_posts()): the_post(); ?>
<article id="<?php echo $post->post_name; ?>" class="post<?php sticky_class(); ?>" itemscope itemtype="http://schema.org/Article">
    <?php if ($count == 1): ?>
    <?php if (has_post_thumbnail($post->ID)): ?>
    <?php $image = wp_get_attachment_image_src(get_post_thumbnail_id($post->ID), 'blog-thumbnail'); ?>
    <a class="banner" href="<?php the_permalink(); ?>"><img src="<?php echo $image[0]; ?>" width="<?php echo $image[1]; ?>" height="<?php echo $image[2]; ?>" alt="<?php the_title(); ?>" itemprop="image"></a>
    <?php endif; ?>
    <?php endif; $count++; ?>
    <header>
        <h1 itemprop="name"><a href="<?php the_permalink(); ?>" title="<?php printf(esc_attr__('%s', 'schema'), the_title_attribute('echo=0')); ?>" rel="bookmark" itemprop="url"><?php the_title(); ?></a></h1>
    </header>
    <footer>
        <?php schema_posted_on(); ?>
    </footer>
    <?php if (is_archive() || is_search()): ?>
    <?php the_excerpt(); ?>
    <?php else: ?>
    <?php the_content(__('Continue reading <span class="meta-nav">&rarr;</span>', 'schema')); ?>
    <?php wp_link_pages(array('before' => '<div class="page-link">' . __('Pages:', 'schema'), 'after' => '</div>')); ?>
    <?php endif; ?>
    <?php edit_post_link(__('Edit', 'schema'), '<p class="edit-link">', '</p>'); ?>
</article>
share|improve this question

1 Answer 1

up vote 0 down vote accepted

You can use the_post_thumbnail() to output the thumbnail image directly.

<?php $count = 1; ?>
<?php while (have_posts()){ the_post(); ?>
<article id="<?php echo $post->post_name; ?>" class="post<?php post_class(); ?>" itemscope itemtype="http://schema.org/Article"><?php
    if ($count == 1 && has_post_thumbnail( get_the_ID() ) ){
        the_post_thumbnail('blog-thumbnail', array('class' => 'banner', 'item_prop' => 'image'));
    } 
    $count++; ?>
    <header>
        <h1 itemprop="name"><a href="<?php the_permalink(); ?>" title="<?php printf(esc_attr__('%s', 'schema'), the_title_attribute('echo=0')); ?>" rel="bookmark" itemprop="url"><?php the_title(); ?></a></h1>
    </header>
    <footer>
        <?php schema_posted_on(); ?>
    </footer><?php
    if (is_archive() || is_search()){
        the_excerpt();
    } else {
        the_content(__('Continue reading <span class="meta-nav">&rarr;</span>', 'schema'));
        wp_link_pages(array('before' => '<div class="page-link">' . __('Pages:', 'schema'), 'after' => '</div>'));
    }
    edit_post_link(__('Edit', 'schema'), '<p class="edit-link">', '</p>'); ?>
</article>
<?php } ?> 
share|improve this answer
    
I have to say like your code snippet. It's cleaner than WordPress currently uses. (the format I'm currently using) The question wasn't however how to shorten things, or how to use the the_post_thumbnail(), but why it isn't showing on a first post. I still hope you could assist. –  Cor van Noorloos Dec 5 '11 at 19:28
    
Ok are you using sticky posts? If yes then should the sticky post be counted as the first post or excluding sticky post the first post only should show the featured image ? –  Joshua Abenazer Dec 5 '11 at 19:33
    
Hi Joshua, a bit of both really. And I've probably messed up my first explanation... I'd only like the featured image to show if a post is the first post OR if a post is a sticky post (AND first of all sticky posts only). –  Cor van Noorloos Dec 5 '11 at 20:04
    
Hmm the snippet works for me... Do you have a featured image set for the first post? –  Joshua Abenazer Dec 6 '11 at 3:01
    
Hello Joshua, in short: I'm a dumb... I sort of mixed the first and the last post. Very much appreciated! –  Cor van Noorloos Dec 6 '11 at 16:34

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.