我正在使用WordPress中的Set featured Image
在每篇博文的顶部显示图片。我使用的代码很简单但是可以完成工作
<?php get_header(); ?>
<?php if (have_posts()): while (have_posts()) : the_post(); ?>
<div class="featured-image">
<div class="featured-image-wrap">
<!-- Post Thumbnail -->
<?php if ( has_post_thumbnail()) : // Check if Thumbnail exists ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<?php the_post_thumbnail(); // Fullsize image for the single post ?>
</a>
<?php endif; ?>
<!-- /Post Thumbnail -->
</div>
</div>
<!-- Section -->
<section class="blog">
<!-- Article -->
<article class="blog-post" id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
<!-- Post Title -->
<h1>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>"><?php the_title(); ?></a>
</h1>
<!-- /Post Title -->
<!-- Post Details -->
<span class="date"><h2>Written By <?php the_author(); ?> on <?php the_time('F j, Y'); ?></h2></span>
<!-- /Post Details -->
<?php the_content(); // Dynamic Content ?>
<br class="clear">
<?php the_tags( __( 'Tags: ', 'html5blank' ), ', ', '<br>'); // Separated by commas with a line break at the end ?>
<p><?php _e( 'Categorised in: ', 'html5blank' ); the_category(', '); // Separated by commas ?></p>
<?php edit_post_link(); // Always handy to have Edit Post Links available ?>
</article>
<!-- /Article -->
<?php endwhile; ?>
<?php else: ?>
<!-- Article -->
<article>
<h1><?php _e( 'Sorry, nothing to display.', 'html5blank' ); ?></h1>
</article>
<!-- /Article -->
<?php endif; ?>
</section>
<!-- /Section -->
<div class="finished">
<h2>Finished! Any Questions?</h2>
<p>If you have any questions please feel free to email me or <a href="http://twitter.com/joshua_hornby">Tweet me</a>. If you learnt anything in this lesson or found it useful please share with your friends.</p>
<a href="https://twitter.com/share" class="twitter-share-button" data-via="joshua_hornby" data-size="large">Tweet</a>
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script>
</div>
<div class="blog-footer">
<aside class="footer-content">
<div class="dark-btn-footer-twitter"><a href="http://twitter.com/joshua_hornby" target="_blank">@joshua_hornby</a></div>
<div class="dark-btn-footer-twitter bottom"><a href="http://designer-school.com">More Posts</a></div>
</aside>
</div>
虽然我在编辑帖子时更改了图像,但它会在所有帖子上更改它。有没有办法让每个帖子都有自己的特色图片?
答案 0 :(得分:1)
我想注意the_post_thumbnail()
函数应该在single.php的循环中使用:
if (have_posts()) :
while (have_posts()) :
if ( has_post_thumbnail()) : ?>
<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
<?php the_post_thumbnail(); ?>
</a>
<?php endif;
endwhile;
endif;
如果不是(即在single.php文件中某处的循环之外),请使用相同的代码但使用函数get_the_post_thumbnail($id, $size, $attr )
并在第一个参数中指定相关帖子的id。其他2个参数是可选的
我希望它有所帮助,如果没有,最好发送一个显示更多single.php文件代码的编辑。