• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

怎么为WordPress上下篇文章链接添加缩略图

武飞扬头像
PHP中文网
帮助40

为WordPress上下篇文章链接添加缩略图

大部分WordPress主题都会在正文下面添加上下篇文章的链接,可以通过下面的代码给这个链接再加个缩略图,让其更醒目。

将下面代码添加到正文模板文件的适当位置即可。

代码一

默认调用文章100×100的特色图像。

<div   class="navigation">
<?php $prevPost = get_previous_post(true);
if($prevPost) ?>
<div class="nav-box previous">
<?php previous_post_link('&laquo; &laquo; Previous Post:', 'yes'); ?>
<?php $prevthumbnail = get_the_post_thumbnail($prevPost->ID, array(100,100) );?>
<?php previous_post_link('%link',"$prevthumbnail <p>%title</p>", TRUE); ?>
</div>
<?php $nextPost = get_next_post(true);
if($nextPost) ?>
<div   style="float:right;">
<?php previous_post_link('&raquo; &raquo; Next Post:', 'yes'); ?>
<?php $nextthumbnail = get_the_post_thumbnail($nextPost->ID, array(100,100) ); ?>
<?php next_post_link('%link',"$nextthumbnail <p>%title</p>", TRUE); ?>
</div>
<?php ?>
</div>

配套样式

#post-nav{clear: both; height: 100px; margin: 0 0 70px;}
#post-nav .nav-box{background: #e9e9e9; padding: 10px;}
#post-nav img{float: left; margin: 0 10px 0 0;}
 
#post-nav p{margin: 0 10px; font-size: 11px; vertical-align: middle;}
#post-nav .previous{float: left; vertical-align: middle; width: 300px; height: 120px;}
#post-nav .next{float: right; width: 300px; height: 120px;}

代码二

除了调用特色图像,并显示文章发表时间,稍加修改还可以添加更多的文章信息,包括自定义缩略图、文章简要等。

<div id="post-nav">
<?php $prevPost = get_previous_post(true);
if($prevPost) {
$args = array(
'posts_per_page' => 1,
'include' => $prevPost->ID
);
$prevPost = get_posts($args);
foreach ($prevPost as $post) {
setup_postdata($post);
?>
<div class="post-previous">
<a   href="https://www.swvq.com/cms/wordpress/<?php the_permalink(); ?>">&laquo; Previous Story</a>
<a href="https://www.swvq.com/cms/wordpress/<?php the_permalink(); ?>"><?php the_post_thumbnail('thumbnail'); ?></a>
<h4><a href="https://www.swvq.com/cms/wordpress/<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
<small><?php the_date('F j, Y'); ?></small>
</div>
<?php
wp_reset_postdata();
} //end foreach
} // end if
 
$nextPost = get_next_post(true);
if($nextPost) {
$args = array(
'posts_per_page' => 1,
'include' => $nextPost->ID
);
$nextPost = get_posts($args);
foreach ($nextPost as $post) {
setup_postdata($post);
?>
<div class="post-next">
<a   href="https://www.swvq.com/cms/wordpress/<?php the_permalink(); ?>">Next Story &raquo;</a>
<a href="https://www.swvq.com/cms/wordpress/<?php the_permalink(); ?>"><?php the_post_thumbnail('thumbnail'); ?></a>
<h4><a href="https://www.swvq.com/cms/wordpress/<?php the_permalink(); ?>"><?php the_title(); ?></a></h4>
<small><?php the_date('F j, Y'); ?></strong>
</div>
<?php
wp_reset_postdata();
} //end foreach
} // end if
?>
</div>

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanfifjj
系列文章
更多 icon
同类精品
更多 icon
继续加载