Hello again.
Is anyone up for the challenge of helping me? I really respect the knowledge of everyone on the forum and love their willingness to help.
Thanks!
Graphic outlining my issue is here:
http://www3.ouragility.com/wp-content/uploads/2009/03/Untitled-1.png
function truncate($string, $max = 20, $replacement = '')
{
if (strlen($string) <= $max)
{
return $string;
}
$leave = $max - strlen ($replacement);
return substr_replace($string, $replacement, $leave);
}
echo truncate('something to truncate',10,'...');
Would output…
somethi...
Just pass what you want to trim down into the function…
Pulled directly off the PHP website, bear in mind whatever you add onto the end (3 dots/periods in example) will count toward the character count (10 in example).
OK. I get the gist of this…but am not completely clear on how/where to add that function to the original code. Any help? Please?
<?php
function truncate($string, $max = 20, $replacement = '')
{
if (strlen($string) <= $max)
{
return $string;
}
$leave = $max - strlen ($replacement);
return substr_replace($string, $replacement, $leave);
}
$sql='SELECT post_title, comment_count, guid
FROM wp_posts
ORDER BY comment_count DESC
LIMIT 4;';
$results = $wpdb->get_results($sql);
foreach ($results as $r) {
echo '<li><a href="' . $r->guid . '" title="' .$r->post_title . '"> ' . truncate($r->post_title,20,'..') .
' (' . $r->comment_count . ')</a></li>';
}
?>
Should do it…
Perfect. Thank you so much!
You’re welcome…
If you want to re-use that function then it may be more ideal to move the function part into your theme functions.php, just this part…
function truncate($string, $max = 20, $replacement = '')
{
if (strlen($string) <= $max)
{
return $string;
}
$leave = $max - strlen ($replacement);
return substr_replace($string, $replacement, $leave);
}