2013-04-09 50 views
0

我使用Set featured Image在WordPress的每篇博客文章的頂部顯示一個圖像。我使用的代碼很簡單,但編輯它改變了它的所有帖子後,當沒有工作允許每個博客帖子有不同的特色照片

<?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當過我改變形象。有沒有辦法讓每個帖子都有自己的特徵圖像?

+0

當然你正在使用這個在你的single.php循環裏面。只是爲了向你確認。 – 2013-04-09 18:40:55

+0

是的,這是在我的single.php – joshuahornby10 2013-04-09 18:41:35

+0

裏面循環?因爲如果沒有,你應該使用get_the_post_thumbnail($ id,$ size,$ attr)而不是the_post_thumbnail() – 2013-04-09 18:42:12

回答

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文件的更多的代碼編輯。

+0

我想你可能會想念我想問的問題,我希望每個博客帖子都有自己的圖片,所以發佈1作爲圖片x和發佈2圖片y。我已經編輯了OP來顯示我的整個single.php – joshuahornby10 2013-04-09 18:59:56

+0

我看過你的代碼,它看起來是正確的,但我並沒有錯過你所問的,因爲我確信定位循環和函數可以導致你所有的帖子顯示同一個縮略圖。 – 2013-04-09 19:06:14

+0

我希望每個帖子都能顯示不同的縮略圖。 – joshuahornby10 2013-04-09 19:14:47

相關問題