Title: Function Reference/previous post link
Last modified: August 20, 2016

---

# Function Reference/previous post link

 *  Resolved [baszer](https://wordpress.org/support/users/baszer/)
 * (@baszer)
 * [14 years, 6 months ago](https://wordpress.org/support/topic/function-referencewp-link-pages/)
 * Hello,
 * i have this:
 * `<div class="nav-previous"><?php previous_post_link( '%link', '&laquo; %title')?
   ></div>`
 * I really like this! Accept when the title of the post is to long, it goes to 
   the next line. Is it also possible to shorten the url when it gets longer than
   a certain amount of characters? (like: This is a very long pos…)
 * because I can’t find anything about this in [the codex of previous post link](http://codex.wordpress.org/Function_Reference/previous_post_link)

Viewing 7 replies - 1 through 7 (of 7 total)

 *  [Jeremy Herve](https://wordpress.org/support/users/hd-j/)
 * (@hd-j)
 * [14 years, 6 months ago](https://wordpress.org/support/topic/function-referencewp-link-pages/#post-2392526)
 * You might want to have a look at this thread, that should help you.
 * [http://wordpress.org/support/topic/substr-still-echoing-whole-string-help?replies=8](http://wordpress.org/support/topic/substr-still-echoing-whole-string-help?replies=8)
 *  Thread Starter [baszer](https://wordpress.org/support/users/baszer/)
 * (@baszer)
 * [14 years, 6 months ago](https://wordpress.org/support/topic/function-referencewp-link-pages/#post-2392551)
 * thanks! the downside is that it also kills the « (and not this one, but the one
   to the right)
 * Is there a way to have this:
 * longtit… ->
 *  [Jeremy Herve](https://wordpress.org/support/users/hd-j/)
 * (@hd-j)
 * [14 years, 6 months ago](https://wordpress.org/support/topic/function-referencewp-link-pages/#post-2392556)
 * You could still place it outside your the post link, like `<div class="nav-previous"
   >&laquo;<?php previous_post_link( '%link', '&laquo; %title' ) ?></div>`
 *  Thread Starter [baszer](https://wordpress.org/support/users/baszer/)
 * (@baszer)
 * [14 years, 6 months ago](https://wordpress.org/support/topic/function-referencewp-link-pages/#post-2392558)
 * Thanks Jeremy! This did the trick, for everyone who comes to this topic:
 *     ```
       add the following to your function.php
       function filter_shorten_linktext($linkstring,$link) {
       	$characters = 33;
       	preg_match('/<a.*?>(.*?)<\/a>/is',$linkstring,$matches);
       	$displayedTitle = $matches[1];
       	$newTitle = shorten_with_ellipsis($displayedTitle,$characters);
       	return str_replace('>'.$displayedTitle.'<','>'.$newTitle.'<',$linkstring);
       }
   
       function shorten_with_ellipsis($inputstring,$characters) {
         return (strlen($inputstring) >= $characters) ? substr($inputstring,0,($characters-3)) . '...' : $inputstring;
       }
   
       // This adds filters to the next and previous links, using the above functions
       // to shorten the text displayed in the post-navigation bar. The last 2 arguments
       // are necessary; the last one is the crucial one. Saying "2" means the function
       // "filter_shorten_linktext()" takes 2 arguments. If you don't say so here, the
       // hook won't pass them when it's called and you'll get a PHP error.
       add_filter('previous_post_link','filter_shorten_linktext',10,2);
       add_filter('next_post_link','filter_shorten_linktext',10,2);
       ```
   
 * and the following on the place where you want the navigations
 *     ```
       <div id="nav-below" class="navigation">
       <div class="nav-previous">&laquo; <?php previous_post_link ( '%link', '%title' ) ?></div>
       <div class="nav-next"><?php next_post_link( '%link', '%title &raquo;' ) ?> &raquo;</div>
       </div>
       ```
   
 *  Thread Starter [baszer](https://wordpress.org/support/users/baszer/)
 * (@baszer)
 * [14 years, 6 months ago](https://wordpress.org/support/topic/function-referencewp-link-pages/#post-2392559)
 * aaaargh, still not perfect. Now I got the » still visible even if you are on 
   the latest post!
 *  Thread Starter [baszer](https://wordpress.org/support/users/baszer/)
 * (@baszer)
 * [14 years, 6 months ago](https://wordpress.org/support/topic/function-referencewp-link-pages/#post-2392731)
 * anybody an idea how to get rit of the » when you are on the last post?
 *  Thread Starter [baszer](https://wordpress.org/support/users/baszer/)
 * (@baszer)
 * [14 years, 4 months ago](https://wordpress.org/support/topic/function-referencewp-link-pages/#post-2392801)
 * fixed:
 *     ```
       <div id="nav-above" class="navigation">
       <div class="nav-previous"><?php previous_post_link ( '&laquo; %link', '%title' ) ?></div>
       <div class="nav-next"><?php next_post_link( '%link &raquo;', '%title' ) ?> </div>
       </div>
       ```
   

Viewing 7 replies - 1 through 7 (of 7 total)

The topic ‘Function Reference/previous post link’ is closed to new replies.

## Tags

 * [previous post link](https://wordpress.org/support/topic-tag/previous-post-link/)

 * 7 replies
 * 2 participants
 * Last reply from: [baszer](https://wordpress.org/support/users/baszer/)
 * Last activity: [14 years, 4 months ago](https://wordpress.org/support/topic/function-referencewp-link-pages/#post-2392801)
 * Status: resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
