<?php
$trim_length = 21; //desired length of text to display
$custom_field = 'your custom field key here';
$value = get_post_meta($post->ID, $custom_field, true);
if ($value) {
echo '<p>I want to use: ' . rtrim(substr($value,0,$trim_length)) . '...</p>';
}
?>
Hi MichaelH,
I can’t seem the get this to work. When I put this in my functions.php file I got an error due to the <?php that’s at the beginning. I placed the code exactly like this to not get an error:
$trim_length = 21; //desired length of text to display
$custom_field = 'your custom field key here';
$value = get_post_meta($post->ID, $custom_field, true);
if ($value) {
echo '<p>I want to use: ' . rtrim(substr($value,0,$trim_length)) . '...</p>';
}
but it still doesn’t work.. why am I doing wrong?
Correction, this is how I placed my code:
$trim_length = 21; //desired length of text to display
$custom_field = 'drink description';
$value = get_post_meta($post->ID, $custom_field, true);
if ($value) {
echo '<p>I want to use: ' . rtrim(substr($value,0,$trim_length)) . '...</p>';
}
I placed the code in my loop so that it pulls the custom field on every post like so:
<?php
$trim_length = 5; //desired length of text to display
$custom_field = 'drink description';
$value = get_post_meta($post->ID, $custom_field, true);
if ($value) {
echo rtrim(substr($value,0,$trim_length));
}
?>
but no dice….
got it to work. It was working this last time I tried, i just hadn’t noticed it… thanks for the code!