在下周公子

  • 首页
  • 域名收藏
  • SEM搜索引擎营销
  • 操作系统
    • 服务器应用
  • 特色专题
    • 跑步运动
  • VPS主机推荐
  • 搜索
在下周公子
三十而立,学以致用
  1. 首页
  2. Wordpress技巧
  3. 正文

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

2015年5月5日 2032点热度 1人点赞 0条评论

当你发现,您喜欢的主题没有上一篇,或者下一篇的链接时,你会发现很纠结。如此只能利用,官方提供的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>

实际效果图:
请看文章分享代码下方。

标签: 暂无
最后更新:2019年6月15日

zhou

这个人很懒,什么都没留下

点赞
< 上一篇
下一篇 >

COPYRIGHT © 2021 zhoubin.net. ALL RIGHTS RESERVED.

Theme Kratos Made By Seaton Jiang

浙ICP备09065757号-10