doupa8922 2013-04-09 18:21 采纳率: 100%
浏览 29
已采纳

允许每篇博文都有不同的精选图片

I am using the Set featured Image in WordPress to display a image at the top of each blog post. The code I am using is simple but does the job

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

Alhough when ever I change the image when editing a post it changes it on all posts. Is there a way to allow each post to have its own feature image?

  • 写回答

1条回答 默认 最新

  • dongtuan5367 2013-04-09 18:54
    关注

    I want to notice that the_post_thumbnail() function should be used inside of the loop of 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;
    

    If not (i.e outside of loop somewhere in the single.php file), use the same code but with the function get_the_post_thumbnail($id, $size, $attr ) and specify the id of the concerned post in the first parameter. the other 2 parameters are optional

    I hope it helps, if not, it is better you send an edit showing more code of your single.php file.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了