php 如何在wordpress中获取下一个/上一个帖子的href和标题

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11379549/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 00:23:34  来源:igfitidea点击:

how to get next/previous post hrefs and titles in wordpress

phpwordpress

提问by 32bitfloat

It's about the view of a single post. I'm trying to set the links for previous and next blogposts like this way:

这是关于单个帖子的视图。我正在尝试以这种方式设置上一篇和下一篇博文的链接:

<a class="prevpost" href="linktoprevpost" title="prev post's title">&nbsp;</a>
<a class="nextpost" href="linktonextpost" title="next post's title">&nbsp;</a>

where both links get an image as background by using display: block and specified width and height. The titles of the linked posts should be accessible via the title-attribute of the a-tags, so that users can see them by hovering.
I also want to restrict the linked posts on the current category. So I need to find a way to get

其中两个链接都通过使用 display: block 和指定的宽度和高度来获取图像作为背景。链接帖子的标题应该可以通过 a 标签的标题属性访问,以便用户可以通过悬停来查看它们。
我还想限制当前类别上的链接帖子。所以我需要找到一种方法来获得

  1. an a-tag with the href of the previous/next post
  2. which is in the same category as the one currently viewed
  3. without inner text because of the backgroundimage
  4. with the previous/next post name in title-attribute
  5. with a custom css-class
  1. 带有上一篇/下一篇文章的 href 的 a 标签
  2. 与当前查看的属于同一类别
  3. 由于背景图像而没有内部文本
  4. 使用标题属性中的上一个/下一个帖子名称
  5. 使用自定义 css 类


The category matching needs to be only the first level because I divided my page into 3 main categories. I'm using


类别匹配只需第一级,因为我将页面分为 3 个主要类别。我正在使用

$a = get_the_category(get_the_ID());
$cat = $a[0]->name;

for getting the first category's name and setting it as additional body-class in header.php. Maybe I could reuse that?

I also found out that using previous_post_link() and next_post_link() like this way

用于获取第一个类别的名称并将其设置为 header.php 中的附加主体类。也许我可以重复使用它?

我还发现像这样使用 previous_post_link() 和 next_post_link()

next_post_link('%link', '', TRUE);

gives me the posts of the same category without inner content, so 1 & 2 & 3 would be solved. But it seems, to get 4 & 5 too I'll need another way.

给我没有内部内容的相同类别的帖子,所以 1 & 2 & 3 将被解决。但看来,要获得 4 和 5,我还需要另一种方式。

Using Wordpress Version 3.4.1.

使用 Wordpress 3.4.1 版。

回答by Rahman Sharif

No need for functions and filters all you need to do is to use get_adjacent_postinstead of next_post_linkand prev_post_link, Note that get_adjacent_postis used to get previous and next post, you can read about it hereTo get previous post and it's title attribute use this

不需要函数和过滤器,你需要做的就是使用andget_adjacent_post代替next_post_linkand prev_post_link,注意它get_adjacent_post用于获取上一篇和下一篇文章,你可以在这里阅读它 要获取上一篇文章及其标题属性,请使用 this

$prev_post = get_adjacent_post(false, '', true);
if(!empty($prev_post)) {
echo '<a href="' . get_permalink($prev_post->ID) . '" title="' . $prev_post->post_title . '">' . $prev_post->post_title . '</a>'; }

To get next post and it's title attribute use this

要获取下一篇文章及其标题属性,请使用此

$next_post = get_adjacent_post(false, '', false);
if(!empty($next_post)) {
echo '<a href="' . get_permalink($next_post->ID) . '" title="' . $next_post->post_title . '">' . $next_post->post_title . '</a>'; }

回答by 32bitfloat

Got it.

知道了。

Now this is my code:

现在这是我的代码:

$p = get_adjacent_post(1, '', 1);
if(!empty($p)) echo '<a class="prevpost" href="'.$p->guid.'" title="'.$p->post_title.'">&nbsp</a>';
$n = get_adjacent_post(1, '', 0);
if(!empty($n)) echo '<a class="nextpost" href="'.$n->guid.'" title="'.$n->post_title.'">&nbsp</a>';

The function returns an object of the prev/next post which I can use for generating my links. The first parameter is for restricting the post on the same cat.
I searched in wordpress codex a few times yesterday but didn't come across this function, now stumled upon it by accident.

If someone has a better/simpler/faster method please post to get an accepted answer.

该函数返回上一篇/下一篇文章的一个对象,我可以用它来生成我的链接。第一个参数用于限制同一只猫上的帖子。
我昨天在 wordpress codex 中搜索了几次,但没有遇到这个功能,现在偶然发现了它。

如果有人有更好/更简单/更快的方法,请发布以获得可接受的答案。

回答by maxhud

<?
echo '<a href="'.get_permalink( get_the_ID()-1 ).'" title="'.get_the_title( get_the_ID()-1 ).'">Previous</a>'; 
echo '<a href="'.get_permalink( get_the_ID()+1 ).'" title="'.get_the_title( get_the_ID()-1 ).'">Next</a>';

?>