WordPress.org

Ready to get started?Download WordPress

Forums

Trim text in custom field (2 posts)

  1. nip0n
    Member
    Posted 3 years ago #

    Is is possible to trim text from custom field?

    I wanna show part of text which is entered in custom field. Any way to show trimmed text in theme? Any function similar to wp_trim_excerpt?

  2. Stef Verbeeck
    Member
    Posted 3 years ago #

    I was looking for the same thing. The code below should do the trick:

    <?php
    $trim_length = 5;  //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));
    }
    ?>

    Worked perfectly for me!

Topic Closed

This topic has been closed to new replies.

About this Topic

Tags