我目前正在使用此代码获取上一个和下一个帖子链接和缩略图
<?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 :(得分: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>