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

32b*_*oat 8 php wordpress

这是关于单个帖子的观点.我正在尝试像这样设置上一个和下一个博客帖子的链接:

<a class="prevpost" href="linktoprevpost" title="prev post's title">&nbsp;</a>
<a class="nextpost" href="linktonextpost" title="next post's title">&nbsp;</a>
Run Code Online (Sandbox Code Playgroud)

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

  1. 带有上一个/下一个帖子的href的a-tag
  2. 与当前查看的类别属于同一类别
  3. 没有内部文本,因为backgroundimage
  4. 使用title-attribute中的上一个/下一个帖子名称
  5. 使用自定义css类


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

$a = get_the_category(get_the_ID());
$cat = $a[0]->name;
Run Code Online (Sandbox Code Playgroud)

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

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

next_post_link('%link', '', TRUE);
Run Code Online (Sandbox Code Playgroud)

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

使用Wordpress版本3.4.1.

Rah*_*leh 34

不需要功能和过滤器你需要做的就是使用get_adjacent_post而不是next_post_linkprev_post_link,注意get_adjacent_post用于获取上一篇和下一篇文章,你可以在这里阅读它以获得上一篇 文章及其标题属性使用此

$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>'; }
Run Code Online (Sandbox Code Playgroud)

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

$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>'; }
Run Code Online (Sandbox Code Playgroud)