WordPress:在显示链接之前检查是否有以前的帖子

发布于 2024-08-30 15:46:23 字数 462 浏览 1 评论 0原文

我使用以下代码在我的 WordPress 博客上显示“以前的帖子”链接。

     <nav>
            <ul>
                <li><?php previous_posts_link('Newer Entries &raquo;') ?></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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

浅沫记忆 2024-09-06 15:46:23

您可以尝试类似 get_previous_posts_link 返回 null(虚假值)的操作。

<?php
    if($link = get_previous_posts_link()) {
        echo '<ul><li>'.$link.'</li></ul>';
?>

如果没有任何以前的帖子,

You can try something like this

<?php
    if($link = get_previous_posts_link()) {
        echo '<ul><li>'.$link.'</li></ul>';
?>

get_previous_posts_link returns null (falsy value) if there isn't any previous post.

情感失落者 2024-09-06 15:46:23

需要明确的是:

我认为科林的回答不正确。
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 对我来说仍然很好。

if(get_next_post()) {  }
if(get_previous_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.

if(get_next_post()) {  }
if(get_previous_post()) {  }
地狱即天堂 2024-09-06 15:46:23

没有一个答案对我有用。
我是这样解决的:

$next = get_permalink(get_adjacent_post(false,'',false)); //next post url
$prev= get_permalink(get_adjacent_post(false,'',true)); //previous post url
<?php if (get_the_permalink()!=$prev): ?>
    <a href='<?php echo $prev ?>'>Previous</a>
<?php endif; ?>
<?php if (get_the_permalink()!=$next): ?>
    <a href="<?php echo $next ?>">Next</a>
<?php endif; ?>

None of the answers worked for me.
I solved it this way:

$next = get_permalink(get_adjacent_post(false,'',false)); //next post url
$prev= get_permalink(get_adjacent_post(false,'',true)); //previous post url
<?php if (get_the_permalink()!=$prev): ?>
    <a href='<?php echo $prev ?>'>Previous</a>
<?php endif; ?>
<?php if (get_the_permalink()!=$next): ?>
    <a href="<?php echo $next ?>">Next</a>
<?php endif; ?>
蒲公英的约定 2024-09-06 15:46:23

对于 2013 年检查此内容的人来说,get_previous_post 已被贬值。

http://codex.wordpress.org/Next_and_Previous_Links
http://codex.wordpress.org/Function_Reference/previous_post

我曾经使用过这个:/

if(get_next_post()) { echo 'next'; }
if(get_previous_post()) { echo 'last'; }

但是现在我用这个:)

if(get_next_posts_link()) { echo 'next'; }
if(get_previous_posts_link()) { echo 'last'; }

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 :/

if(get_next_post()) { echo 'next'; }
if(get_previous_post()) { echo 'last'; }

But now I use this :)

if(get_next_posts_link()) { echo 'next'; }
if(get_previous_posts_link()) { echo 'last'; }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文