简体   繁体   English

wordpress:previous_post_link()/ next_post_link()占位符

[英]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 = "&lt;-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 &raquo;'); } 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.

相关问题 Wordpress:将if-else语句插入next_post_link()/ previous_post_link()参数? - Wordpress: Inserting if-else statement into next_post_link() / previous_post_link() parameters? WordPress-使用previous_post_link()和next_post_link()时删除分隔符 - Wordpress - Remove separator when using previous_post_link() and next_post_link() 如何从next_post_link和previous_post_link(Wordpress)中排除某些类别? - How to exclude some Categories from next_post_link and previous_post_link (Wordpress)? Wordpress next_post_link / previous_post_link不属于同一类别 - Wordpress next_post_link/previous_post_link not staying in same category Wordpress:previous_post_link / next_post_link按字母顺序排列? - Wordpress: previous_post_link / next_post_link by alphabetical order? 如何将PHP代码放入wordpress分页功能(previous_post_link,next_post_link等)中? - How to put PHP code in wordpress pagination function (previous_post_link,next_post_link,etc)? WordPress-next_post_link / previous_post_link提供文本而不是链接 - Wordpress - next_post_link/previous_post_link giving text instead of links 如何在简码中显示 previous_post_link () / next_post_link () - Wordpress - How to show previous_post_link () / next_post_link () in shortcode - Wordpress next_post_link previous_post_link在没有下一篇文章时禁用,而不是循环帖子 - next_post_link previous_post_link dont disable when there is no next post, instead loop posts 我在 single.php 中使用 previous_post_link() 和 next_post_link() 的单个帖子导航遇到问题,但不能使用帖子类型“post” - I am getting issues with my single Post navigation in single.php with previous_post_link() and next_post_link() is not working with post type "post"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM