Support » Themes and Templates » Function Reference/previous post link

  • Resolved baszer

    (@baszer)


    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

Viewing 7 replies - 1 through 7 (of 7 total)
  • 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

    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… ->

    You could still place it outside your the post link, like <div class="nav-previous">&laquo;<?php previous_post_link( '%link', '&laquo; %title' ) ?></div>

    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>

    aaaargh, still not perfect. Now I got the » still visible even if you are on the latest post!

    anybody an idea how to get rit of the » when you are on the last post?

    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.