首页 > 解决方案 > Wordpress 发布上一个/下一个链接,仅在上一个上带有图像和标题

问题描述

我正在尝试为上一篇文章和下一篇文章添加箭头图像,但我希望帖子标题仅显示在上一篇文章中,而不是下一篇文章中。

这是我的代码 -

 <div class="nav-next"><?php previous_post_link('%link', ' <img src="/wp-content/themes/azure-basic-child/images/larrow.png" />' , ' %title '); ?></div>

 <div class="nav-previous"><?php next_post_link('%link', ' <img src="/wp-content/themes/azure-basic-child/images/rarrow.png" /> '); ?></div>

这是页面 - https://lucchesipgh.org/word-of-the-week/

我那里有箭头,但没有以前的帖子标题。

标签: wordpressposts

解决方案


您可以使用get_previous_post()get_next_post()https://codex.wordpress.org/Function_Reference/get_previous_post / https://codex.wordpress.org/Function_Reference/get_next_post)。

这些函数将返回帖子对象(https://codex.wordpress.org/Class_Reference/WP_Post)。

如果您需要标题使用echo $post->title(未过滤)或the_title($post->ID)(过滤)。要获得永久链接,请使用echo get_the_permalink($post->ID).


推荐阅读