2013-08-22 36 views
0

我目前使用此代碼來獲得一個和下一個帖子鏈接和縮略圖如何獲取一篇文章首先圖片

 <?php $prevPost = get_previous_post(true); 
      if($prevPost) {?> 
      <div class="nav-box previous" style="float:left;"> 
      <?php $prevthumbnail = catch_that_image($prevPost->ID, array(100,100));}?> 
      <?php previous_post_link('%link',"$prevthumbnail %title", TRUE); ?> 
      </div> 

     <?php $nextPost = get_next_post(true); 
      if($nextPost) { ?> 
     <div class="nav-box next" style="float:right;"> 
      <?php $nextthumbnail = catch_that_image($nextPost->ID, array(100,100)); } ?> 
      <?php next_post_link('%link',"$nextthumbnail %title", TRUE); ?> 
      </div> 

但我想,而不是得到那個職位爲縮略圖的第一張圖像,特色圖片。

我發現這個代碼獲取當前帖子的第一張圖片,但是如何將它與上面的代碼一起使用來獲取上一篇文章或下一篇文章的第一張圖片?

function catch_that_image() { 
global $post, $posts; 
$first_img = ''; 
ob_start(); 
ob_end_clean(); 
$output = preg_match_all('/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches); 
$first_img = $matches [1] [0]; 
return $first_img; 
} 

回答

0

我已經使用這個代碼,自定義職位類型,這會爲你做一個小小的改變工作:

<div class="memberNavigation"> 
    <div class="memberNav"> 
    <?php // Display the thumbnail of the previous post ?> 
    <div class="memberNavPrev"> 
     <?php 
       $prevPost = get_previous_post(); 
       $prevthumbnail = get_the_post_thumbnail($prevPost->ID); 
       $prevtitle = get_the_title($prevPost->ID); ?> 
     <p class="navtitle"> 
     <?php previous_post_link('%link', 'Previous Board Member'); ?> 
     </p> 
     <?php previous_post_link('%link', $prevthumbnail); ?> 
     <h5 class="memberTitle"> 
     <?php previous_post_link('%link', $prevtitle); ?> 
     </h5> 
    </div> 
    <?php // Display the thumbnail of the next post ?> 
    <div class="memberNavNext"> 
     <?php 
       $nextPost = get_next_post(); 
       $nextthumbnail = get_the_post_thumbnail($nextPost->ID); 
       $nexttitle = get_the_title($nextPost->ID); ?> 
     <p class="navtitle"> 
     <?php next_post_link('%link', 'Next Board Member'); ?> 
     </p> 
     <?php next_post_link('%link', $nextthumbnail); ?> 
     <h5 class="memberTitle"> 
     <?php previous_post_link('%link', $nexttitle); ?> 
     </h5> 
    </div> 
    <div class="clearfix"></div> 
    </div> 
</div>