Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 373 Vote(s) - 3.72 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how to get next/previous post hrefs and titles in wordpress

#1
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.<br />
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

<br />
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?
<br /><br />
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.<br /><br />

Using Wordpress Version 3.4.1.
Reply

#2
<?
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>';

?>
Reply

#3
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.<br />
I searched in wordpress codex a few times yesterday but didn't come across this function, now stumled upon it by accident.<br />
<br />
If someone has a better/simpler/faster method please post to get an accepted answer.
Reply

#4
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][1]
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>'; }

[1]:

[To see links please register here]

Reply

#5
step 1 : you just need to add this function below in function.php

```
// its to ssolve single page next and previous liinks
# get_adjacent_post( $in_same_cat = false, $excluded_categories = '', $previous = true )
function echo_next_previous_post_link($fmg_name="link" ,$selector="next")
{

if ($selector=="next") {
$next_post_obj = get_adjacent_post( '', '', false );
$next_post_ID = isset( $next_post_obj->ID ) ? $next_post_obj->ID : '';
$next_post_link = get_permalink( $next_post_ID );
$next_post_title = get_the_title($next_post_ID);
if ($fmg_name=="link") {
echo $next_post_link ;
}
else {
echo $next_post_title ;
}
}
else {
$previous_post_obj = get_adjacent_post( '', '', true );
$previous_post_ID = isset( $previous_post_obj->ID ) ? $previous_post_obj->ID : '';
$previous_post_link = get_permalink( $previous_post_ID );
$previous_post_title = get_the_title($previous_post_ID);
if ($fmg_name=="link") {
echo $previous_post_link ;
}
else {
echo $previous_post_title ;
}
}

}
```

Step 2 : now you can print post url and title in any page you want
```
<a href="<?php echo_next_previous_post_link("link","next"); ?>"><?php echo_next_previous_post_link("title","next"); ?></a>

<a href="<?php echo_next_previous_post_link("link","previous"); ?>"><?php echo_next_previous_post_link("title","previous"); ?></a>
```
Reply

#6
The previous code didn't work for me.
I came to the following

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

<?php
$prev = get_adjacent_post(true,'',true);
$next = get_adjacent_post(true,'',false);
?>

<div>
<a href="<?php echo get_permalink($prev->ID);?>">Previous</a>
<p><?php echo get_the_title($prev->ID );?></p>
</div>

<div>
<a href="<?php echo get_permalink($next->ID);?>">Next</a>
<p><?php echo get_the_title($next->ID );?></p>
</div>

<!-- end snippet -->

Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through