Featured image for first post only

admin2025-06-04  44

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="">
    <?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>

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/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 asked Dec 5, 2011 at 18:32 user5424user5424
Add a comment  | 

1 Answer 1

Reset to default 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/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 } ?> 
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1749032385a315785.html

最新回复(0)