[英]wordpress: previous_post_link() / next_post_link() placeholder
I am having trouble with the previous_post_link()
and next_post_link()
functionality. 我在使用
previous_post_link()
和next_post_link()
功能时遇到了问题。 When there is no previous post, the function previous_post_link()
does not display a link, likewise for the next_post_link()
and the last post. 如果没有上一篇文章,则函数
previous_post_link()
不显示链接,同样适用于next_post_link()
和最后next_post_link()
文章。 I would like to have a placeholder image so that the design stays consistent. 我想有一个占位符图像,以便设计保持一致。
Currently I have images of green arrows pointing left and right, I would like to place an image of a grey arrow if there are no more posts to go back to. 目前我有左右指向的绿色箭头图像,如果没有更多的帖子可以返回,我想放置一个灰色箭头的图像。
Is there a way to use the next_post_link()
/ previous_post_link()
functions but not have the link removed. 有没有办法使用
next_post_link()
/ previous_post_link()
函数,但没有删除链接。
I also wonder if there is a way for the links to cycle, so that if you come to the most recent post, the next post link would bring you back to the first post. 我也想知道链接是否有循环的方式,所以如果你来到最新的帖子,下一个帖子链接会带你回到第一篇文章。
************ UPDATED ************ ************ 更新 ************
Here is the code, based on "silent's" advice (accepted answer) to use get_adjacent_post()
: 以下是基于“silent”的建议(接受的答案)使用
get_adjacent_post()
:
<?php
if(get_adjacent_post(false, '', true)) {
previous_post_link('%link','<img src="larr.gif"/>');
}
else {
echo '<img src="larr2.gif"/>';
};
if(get_adjacent_post(false, '', false)) {
next_post_link('%link','<img src="rarr.gif"/>');
}
else {
echo '<img src="rarr2.gif">';
};
?>
So you can "capture" what next_post_link()
and previous_post_link()
return using ob_start()
and ob_get_clean()
, then apply a conditional to it. 因此,您可以使用
ob_start()
和ob_get_clean()
“捕获” next_post_link()
和previous_post_link()
返回的内容,然后对其应用条件。
Code in practice: 实践中的守则:
$previous_string = "<-Back";
ob_start(); // start output buffering
previous_post_link("%link", $previous_string);
$previous_link = ob_get_clean(); // stop output buffering and store
if ($previous_link == '') {
echo '<span style="color: #ccc">' . $previous_string . '</span>';
} else {
echo $previous_link;
}
I never try this myself. 我自己从不尝试过。 However, you may refer to this post .
但是,您可以参考这篇文章 。 It uses get_adjacent_post() .
它使用get_adjacent_post() 。
Why cant you try the below? 为什么你不能尝试以下?
<div style="float:right"><?php if(get_previous_posts_link()) { previous_posts_link('Newer Entries »'); } else { echo 'No Newer Entries'; } ; ?></div>
Alternatively to display Next and Previous Post Links to Blog post you can easily do that. 另外,您可以轻松地显示博客文章的下一个和上一个帖子链接。 This is very well explained at globinch.com (WordPress Tips : How to Add Next and Previous Post Links to Blog? )
这在globinch.com上得到了很好的解释(WordPress提示:如何添加博客的下一个和上一个帖子链接?)
http://www.globinch.com/2010/10/12/wordpress-tips-how-to-add-next-and-previous-post-links-to-blog/ http://www.globinch.com/2010/10/12/wordpress-tips-how-to-add-next-and-previous-post-links-to-blog/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.