I am using this code here:
<?php
$trim_length = 25; //desired length of text to display
$custom_field = 'my-custom-field-name';
$value = get_post_meta($post->ID, $custom_field, true);
if ($value) {
echo rtrim(substr($value,0,$trim_length));
}
?>
It works – but I would like to have a “(…)” at the end of the trimmed text. And only if the value really was trimmed.
I used if ($value) {
echo rtrim(substr($value,0,$trim_length)) . '(...)';
}
?>
But this makes the “(…)” at the end of every text from custom field..
Thank you!
AD
building on keatch’s answer you only need to trim if its longer the 25 chars so:
Try this code.
You must check if the string was trimmed. If it is, is length is
$trim_length
So, you must add the ‘(…)’ at the end
Since WordPress 3.3 there is a built-in function for this : wp_trim_words() which accepts as arguments the number of words and the chain the add if trimmed.
So you could do something like :