Forums

[resolved] Function Reference/previous post link (8 posts)

  1. baszer
    Member
    Posted 6 months ago #

    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

  2. Jeremy
    Member
    Posted 6 months ago #

    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

  3. baszer
    Member
    Posted 6 months ago #

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

  4. Jeremy
    Member
    Posted 6 months ago #

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

  5. baszer
    Member
    Posted 6 months ago #

    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>
  6. baszer
    Member
    Posted 6 months ago #

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

  7. baszer
    Member
    Posted 6 months ago #

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

  8. baszer
    Member
    Posted 4 months ago #

    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>

Reply

You must log in to post.

About this Topic