WordPress获取上下篇的链接、标题、特色图像

WordPress获取上一篇下一篇文章的时候,官方提供了previous_post_link()next_post_link()两个函数标签,制定当前文章所属分类里面的上下篇文章。如果要获取上下篇文章的其他参数呢,比如链接、标题、特色图像等。

这个问题也是在前几天折腾回归视觉网站让网站内容延迟加载后遗留的第二个问题,一直没有解决掉,后来用其他函数解决了,分享一下。

功能需求

获取上下篇文章标题、链接、特色图像

涉及函数标签

get_next_post、get_previous_post、get_permalink、get_the_title、get_the_post_thumbnail

参考文档

http://codex.wordpress.org/Function_Reference/get_next_post

http://codex.wordpress.org/Function_Reference/get_previous_post

http://codex.wordpress.org/Function_Reference/get_permalink

http://codex.wordpress.org/Function_Reference/get_the_title

http://codex.wordpress.org/Function_Reference/get_the_post_thumbnail

实现代码

<div class="post-PrevNext">
	<?php
		$current_category=get_the_category();//获取当前文章所属分类ID
		$prev_post = get_previous_post($current_category,'');//与当前文章同分类的上一篇文章
		$next_post = get_next_post($current_category,'');//与当前文章同分类的下一篇文章
	?>
	<div class="previous_post_link fl">
		<?php if (!empty( $prev_post )): ?>
		<a href="<?php echo get_permalink( $prev_post->ID ); ?>"><?php echo get_the_post_thumbnail( $prev_post->ID, '', '' ); ?></a> 上一篇: <a href="<?php echo get_permalink( $prev_post->ID ); ?>"><?php echo $prev_post->post_title; ?></a>
		<?php endif; ?>
	</div>
	<div class="next_post_link fr">
		<?php if (!empty( $next_post )): ?>
		<a href="<?php echo get_permalink( $next_post->ID ); ?>"><?php echo get_the_post_thumbnail( $next_post->ID, '', '' ); ?></a> 下一篇: <a href="<?php echo get_permalink( $next_post->ID ); ?>"><?php echo $next_post->post_title; ?></a>
		<?php endif; ?>
	</div>
</div>

效果图解

转载于:https://my.oschina.net/pidandan/blog/100630

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值