2013-08-22 24 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> 
相关问题