Support

Account

Home Forums General Issues Limiting the output of a custom field.

Solved

Limiting the output of a custom field.

  • Hi,

    I want to limit the ‘description’ field output so I can create a summary on one page and get the full description on another. I know where to place my code, I’m just not sure how to limit the character output of my ‘description’ field.

    I have this;
    <?php the_field('description'); ?>

    Lets say I want to limit the output to 20 words, how can I do that easily?

    Thanks
    Danny

    • smspaul

    • January 28, 2014 at 7:32 am

    In this situation, you could try something like this:

    http://stackoverflow.com/questions/12444945/cut-the-content-after-10-words

    • Elliot

    • January 28, 2014 at 9:45 am

    Hi @danfloun @smspaul

    Exactly! And make sure to use get_field instead of the_field to get the value, and then pass it into the function to trim it down.

    Cheers
    E

  • Okay, this is what I did;

        <?php $summary = get_field('description');
              echo substr($summary, 0, 200); ?> 

    That does what I require, thanks.

  • I would recommend using the built in WP function wp_trim_words.

    <?php $excerpt = wp_trim_words( get_field('your_custom_field' ), $num_words = 50, $more = '...' ); ?>

    https://codex.wordpress.org/Function_Reference/wp_trim_words

  • Thanks @lukeclifton, this works really great and is simple.

    • azygo

    • November 22, 2018 at 1:47 pm

    Hello,

    Thank you for solving that. But where should I place this code please ?

  • its worked perfectly
    Thanks

Viewing 8 posts - 1 through 8 (of 8 total)

You must be logged in to reply to this topic.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.