Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

WordPress

Leslie Hui
Leslie Hui
3,330 Points

Trimming the no of characters for the description in Wordpress

Hi,

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. :)

<?php
    $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>
    <?php
    }
?>