• turkishjapan

    (@turkishjapan)


    Hi all.

    I wanna show a custom field value but it need a character limit.

    For example:

    value in custom fields: Lorem Ipsum is simply dummy text of the printing and typesetting industry.

    I want to use: Lorem Ipsum is simply…

Viewing 6 replies - 1 through 6 (of 6 total)
  • MichaelH

    (@michaelh)

    <?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>';
    }

    any help is welcome!

    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!

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘How to character limit for custom fields?’ is closed to new replies.