这是关于一个职位的看法。 我想设置一个和下一个相关博客文章像这样的链接:
<a class="prevpost" href="linktoprevpost" title="prev post's title"> </a>
<a class="nextpost" href="linktonextpost" title="next post's title"> </a>
其中两个链接获得的图像,通过使用显示背景:块和指定的宽度和高度。 链接的帖子的标题应该是通过一个标签的标题属性访问,使用户可以将鼠标悬停看到它们。
我还希望限制对当前类别的链接帖子。 所以,我需要找到一种方式来获得
- 所涉及的标签与前/后旁边的href
- 这是在同一类别作为一个当前观看
- 不因为和backgroundImage的内部文本
- 在标题属性上一个/下一个职位名称
- 用自定义CSS类
类别匹配只需是第一级,因为我分了我的页面分为3个主要类别。 我正在使用
$a = get_the_category(get_the_ID());
$cat = $a[0]->name;
为获得第一类的名字,并将其设置在header.php中作为附加体级。 也许我可以重复使用?
我还发现,使用previous_post_link()和next_post_link()这样的方式
next_post_link('%link', '', TRUE);
给我的同类职位没有内部内容,所以1&2&3将得到解决。 但现在看来,拿到4和5太多,我需要另一种方式。
使用WordPress版本3.4.1。
无需功能和过滤所有你需要做的就是用get_adjacent_post
代替next_post_link
和prev_post_link
,需要注意的是get_adjacent_post
用于获取一个和下一个职位,你可以看到它在这里要获得以前的帖子,它的title属性使用
$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>'; }
为了获得下一个职位,它的title属性使用
$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>'; }
得到它了。
现在,这是我的代码:
$p = get_adjacent_post(1, '', 1);
if(!empty($p)) echo '<a class="prevpost" href="'.$p->guid.'" title="'.$p->post_title.'"> </a>';
$n = get_adjacent_post(1, '', 0);
if(!empty($n)) echo '<a class="nextpost" href="'.$n->guid.'" title="'.$n->post_title.'"> </a>';
该函数返回前一个/下一个职位,我可以用于生成我的链接的对象。 第一个参数是限制在同一猫的帖子。
我在WordPress抄本搜查了昨天几次,但过这个功能,现在意外stumled后,它没有来。
如果有人有更好的/简单/更快的方法,请张贴得到一个公认的答案。
<?
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>';
?>