Featured image for first post only

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.

Read More

(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>

Related posts

Leave a Reply

1 comment

  1. 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 } ?>