简体   繁体   中英

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

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

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>'  );

:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM