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"> </a>
<a class="nextpost" href="linktonextpost" title="next post's title"> </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
- an a-tag with the href of the previous/next post
- which is in the same category as the one currently viewed
- without inner text because of the backgroundimage
- with the previous/next post name in title-attribute
- 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.
Got it.
Now this is my code:
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.
No need for functions and filters all you need to do is to use
get_adjacent_post
instead ofnext_post_link
andprev_post_link
, Note thatget_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 thisTo get next post and it's title attribute use this