Developers: How to limit Title length in a YARPP template

Here is a sample of a template to get you started. It is based on the “Simple” example template provided by YARPP. The example below trims all titles to a max length of 50 characters.

<?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 this template, create a file in your theme’s root folder called ‘yarpp-template-short-titles.php`, and paste the above code into it. You should then see a new custom YARPP template called “Short Titles”.

Was this article helpful?
0 out of 0 found this helpful