如何在wordpress中获取下一个/上一个帖子的href和标题 [英] how to get next/previous post hrefs and titles in wordpress

查看:33
本文介绍了如何在wordpress中获取下一个/上一个帖子的href和标题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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>

其中两个链接都通过使用 display: block 和指定的宽度和高度来获取图像作为背景.链接帖子的标题应该可以通过 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

  1. 带有上一篇/下一篇文章的 href 的 a 标签
  2. 与当前查看的内容属于同一类别
  3. 由于背景图像而没有内部文本
  4. 在标题属性中带有上一个/下一个帖子名称
  5. 使用自定义 css 类


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


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

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

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

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

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

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

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

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.

使用 Wordpress 3.4.1 版.

Using Wordpress Version 3.4.1.

推荐答案

不需要函数和过滤器,您只需使用 get_adjacent_post 而不是 next_post_linkprev_post_link,注意get_adjacent_post用于获取上一篇和下一篇文章,你可以阅读它这里要获取以前的帖子及其标题属性,请使用此

No need for functions and filters all you need to do is to use get_adjacent_post instead of next_post_link and prev_post_link, Note that get_adjacent_post is used to get previous and next post, you can read about it here To get previous post and it's title attribute use 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>'; }

这篇关于如何在wordpress中获取下一个/上一个帖子的href和标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆