Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

php

wordpress

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

  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


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;

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);

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.

Using Wordpress Version 3.4.1.

like image 523
32bitfloat Avatar asked Jul 07 '12 23:07

32bitfloat


2 Answers

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>'; }
like image 184
Rahman Saleh Avatar answered Nov 14 '22 04:11

Rahman Saleh


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.

like image 26
32bitfloat Avatar answered Nov 14 '22 04:11

32bitfloat