Support » Plugins and Hacks » [Resolved] How to shorten wordpress related posts title?

[Resolved] How to shorten wordpress related posts title?

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author PetraP


    Hey ninopro,

    thanks for reaching out – I’ll gladly help!
    First go to “Plugins”->”Editor” and select WordPress Related Posts plugin in the upper right corner.
    Then select the init.php file and replace the “function wp_rp_text_shorten($text, $max_char)” function with this one:

    function wp_rp_text_shorten($text, $max_chars) {
    	$shortened_text = mb_substr($text, 0, $max_chars - strlen(html_entity_decode(WP_RP_EXCERPT_SHORTENED_SYMBOL, ENT_QUOTES, 'UTF-8') + 2));
    	$shortened_words = explode(" ", $shortened_text);
    	$shortened_size = count($shortened_words);
    	if ($shortened_size > 1) {
    		$shortened_words = array_slice($shortened_words, 0, $shortened_size - 1);
    		$shortened_text = implode(" ", $shortened_words);
    	return $shortened_text . WP_RP_EXCERPT_SHORTENED_SYMBOL; //'...';

    After that find the line $output .= '<a href="' . $post_url . '" class="wp_rp_title">' . wptexturize($related_post->post_title) . '</a>';
    and replace it with this one:
    $output .= '<a href="' . $post_url . '" class="wp_rp_title">' . wp_rp_text_shorten(wptexturize($related_post->post_title),15) . '</a>';

    You can change that number 15 (title length) to whatever suits you.

    Let me know how it goes, take care and have a nice day,

    Thank you for the above info, it was just what I needed!

    Actually, it’s not quite working. It is shortening posts that only have about 14 characters and I have it set to 65. Surely if it’s under 65 it won’t need to shorten and yet there are posts well under 65 characters that are still being shortened unnecessarily?

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘[Resolved] How to shorten wordpress related posts title?’ is closed to new replies.