How do I display a title excerpt in next post link wordpress

I am having problems displaying a title excerpt on a next post link.

If the characters are more than 30 for the next posts title i would just like it to show ‘…’

Read More

This is the code I use for title excerpts

 <?php short_title('...', 25); ?>

And this is the code I use for next post links

 <?php next_post_link( '<span class="pn-a">%link</span>', '<span class="pn-a">%title</span>' ) ?>

Short Title Function

function short_title($after = '', $length) {
$mytitle = get_the_title();
if ( strlen($mytitle) > $length ) {
$mytitle = substr($mytitle,0,$length);
echo $mytitle . $after;
} else {
echo $mytitle;
}

}

Any help?

Thanks

Related posts

Leave a Reply

1 comment

  1. Here you are 🙂

    Change your function like this, remove the echo and just return the value for the title

    function short_title_next_post($after = '', $length) {
        $next = get_adjacent_post(1, '', 0);
        $mytitle = $next->post_title;
        if ( strlen($mytitle) > $length ) {
            $mytitle = substr($mytitle,0,$length);
            return $mytitle . $after;
        } else {
            return $mytitle;
        }
    }
    

    And then in the next_post_link simply call that function

    next_post_link( '<span class="pn-a">%link</span>', '<span class="pn-a">' . short_title_next_post('...', 25) . '</span>'  );
    

    🙂