$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>Featured image for first post only|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

Featured image for first post only

matteradmin10PV0评论

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

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far