WordPress:在显示链接之前检查是否有以前的帖子
我使用以下代码在我的 WordPress 博客上显示“以前的帖子”链接。
<nav>
<ul>
<li><?php previous_posts_link('Newer Entries »') ?></li>
</ul
</nav>
问题是,当没有任何以前的帖子时,虽然链接不显示,但我仍然会
<nav>
<ul>
<li><</li>
</ul
</nav>
打印出来。是否有一个 if() 语句我可以将其全部包围起来,以便它检查是否有任何以前的帖子,并且仅在有时才将其打印出来?
I'm using the following code to display a 'previous posts' link on my Wordpress blog.
<nav>
<ul>
<li><?php previous_posts_link('Newer Entries »') ?></li>
</ul
</nav>
Problem is, when there ARN'T any previous posts, while the link doesn't display, I still get
<nav>
<ul>
<li><</li>
</ul
</nav>
Printed out. Is there an if() statement I could wrap around it all so it checks if there are any previous posts, and only prints it out if there are?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以尝试类似
get_previous_posts_link
返回 null(虚假值)的操作。如果没有任何以前的帖子,
You can try something like this
get_previous_posts_link
returns null (falsy value) if there isn't any previous post.需要明确的是:
我认为科林的回答不正确。
get_previous_post 并未被弃用,previous_post 已被弃用。
http://codex.wordpress.org/Function_Reference/get_previous_post
http://codex.wordpress.org/Function_Reference/previous_post
对于我来说,使用 get_next_post 对我来说仍然很好。
Just to be clear:
Colin's answer isn't correct in my opinion.
get_previous_post is not deprecated, previous_post is.
http://codex.wordpress.org/Function_Reference/get_previous_post
http://codex.wordpress.org/Function_Reference/previous_post
For me the use of get_next_post works still fine for me.
没有一个答案对我有用。
我是这样解决的:
None of the answers worked for me.
I solved it this way:
对于 2013 年检查此内容的人来说,get_previous_post 已被贬值。
http://codex.wordpress.org/Next_and_Previous_Links
http://codex.wordpress.org/Function_Reference/previous_post
我曾经使用过这个:/
但是现在我用这个:)
for people checking this in 2013, get_previous_post has been depreciated.
http://codex.wordpress.org/Next_and_Previous_Links
http://codex.wordpress.org/Function_Reference/previous_post
I used to use this :/
But now I use this :)