Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial


Leslie Hui
Leslie Hui
3,330 Points

Trimming the no of characters for the description in Wordpress


I'm learning "How to Build a WordPress Theme' now and would like to know how i can limit the no. of characters to display for the description in the Work page.

For this line of code <?php the_field('description'); ?>

Any advise please, Zac Gordon?

1 Answer

Leslie Hui
Leslie Hui
3,330 Points

Thanks Gautam Thapar! I actually solved it before you replied. :)

    $trim_length = 104;  //desired length of text to display
    $more = '… '; 
    $custom_field = 'description';
    $value = get_post_meta($post->ID, $custom_field, true);
    if ($value) { ?>
    <p><?php echo rtrim(substr($value,0,$trim_length));?> </p>