Support » Plugin: YARPP - Yet Another Related Posts Plugin » Limit number of characters within titles?

  • Resolved jh20001

    (@jh20001)


    Is there a way to limit the number of characters within each displayed title result? I have it set to “Simple” template as I play around with it. Some of the titles run off to a second line, some don’t. This makes the display of it look messy. So I was wondering if I could limit the number of characters to like 50 with “…” after the text if needed to keep each result to a single line and looking neat šŸ™‚

    • This topic was modified 11 months, 3 weeks ago by jh20001.
Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Author Michael Nelson

    (@mnelson4)

    Hi @jh20001, yes you can do it but it would either take some fancy work with filters or a custom template. Here’s a sample of a template to get you started, based off the “Simple” example template provided by YARPP:

    
    <?php
    /*
    YARPP Template: Short Titles
    Author: YARPP Team
    Description: A simple example YARPP template.
    */
    function yarpp_trim_title($title){
    	$max = 50;
    	if(strlen($title) > $max) {
    		$title = substr( $title, 0, $max ) . '&hellip;';
    	}
    	return $title;
    }
    ?><h3>Related Posts</h3>
    <?php if (have_posts()):?>
    	<ol>
    		<?php while (have_posts()) : the_post(); ?>
    			<li><a href="<?php the_permalink() ?>" rel="bookmark"><?php echo yarpp_trim_title(get_the_title()); ?></a><!-- (<?php the_score(); ?>)--></li>
    		<?php endwhile; ?>
    	</ol>
    <?php else: ?>
    	<p>No related posts.</p>
    <?php endif; ?>
    
    

    To use it, create a file in your theme’s root folder called ‘yarpp-template-short-titles.php`, and paste that into it. Then you should see a new custom YARPP template called “Short Titles”.

    Let me know if you have questions!

    Thread Starter jh20001

    (@jh20001)

    Great, thanks. I’ll give it a try. If it works, I’ll build it up to something nice. I’m good at the visuals side of things, but when it gets deeper into the code, I brace for help šŸ™‚

    • This reply was modified 11 months, 3 weeks ago by jh20001.
    Thread Starter jh20001

    (@jh20001)

    Update: Works well enough, thank you. I see sometimes a title might be cut to early … (without hitting the max), but it’s fine. It seems to be rare and may only be happening when the following line after it hits the limit. Either it works and looks nice and I was able to format around it to match the theme of things. Thanks again šŸ™‚

    Plugin Author yarpp

    (@jeffparker)

    @jh20001 thanks for confirming!

    btw, you can also use 100% CSS to restrict title length. For example:
    https://css-tricks.com/snippets/css/truncate-string-with-ellipsis/

    Thread Starter jh20001

    (@jh20001)

    Update: Actually, I had to revert back. For some reason, the “preview” screen for a new post is thrown out of whack when this is in use. Switching back, the preview screen is fine. Everything else was ok (the actual submitted post and everything else). It’s just the preview page that was affected. Really strange.

    I may have to try the CSS approach

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Limit number of characters within titles?’ is closed to new replies.