WordPress Related Posts
[resolved] How to shorten wordpress related posts title? (4 posts)

  1. ninopro
    Posted 2 years ago #

    Hi, is there a way to shorten the title of the related posts?


  2. PetraP
    Plugin Author

    Posted 2 years ago #

    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,

  3. sparx
    Posted 1 year ago #

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

  4. sparx
    Posted 1 year ago #

    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?

Topic Closed

This topic has been closed to new replies.

About this Plugin

  • WordPress Related Posts
  • Frequently Asked Questions
  • Support Threads
  • Reviews

About this Topic


No tags yet.