WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am customizing a widget used for displaying testimonials. The widget is working fine, except it shows all content and i want to limit the text/characters so the footer of the website looks balance. Here is the widget code snippet:

function widget( $args, $instance ) {
    extract( $args );

    $title = $instance['title'] ;
    $count = $instance['count'];
    $url = $instance['url'];

    echo $before_widget;
    echo '<div class="testimonials-container">'; 
    if ( $title )
        echo '<h3 class="replace"><a href="' . $url . '">' . $title . '</a></h3>' ;
        $query = new WP_Query();
        <?php if ($query->have_posts()) : while ($query->have_posts()) : $query->the_post(); ?>
            <div class="testimonials-content">
            <?php limit_text(the_content(), 15); ?>
            <span class="testimonial-name"><em>         
            <?php echo get_post_meta(get_the_ID(), 'pkb_author_name', true); ?></em></span>
        <?php endwhile; endif;?>

To truncate it, i use this:

function limit_text($text, $limit) {
    if (strlen($text) > $limit) {
        $words = str_word_count($text, 2);
        $pos = array_keys($words);
        $text = substr($text, 0, $pos[$limit]) . '...';

    return $text;

Its not working. What am i doing wrong here?

share|improve this question
Try get_the_content() – Brian Fegter Jan 4 '12 at 4:14
hmm not working :( – Dzul Jan 4 '12 at 4:23
up vote 1 down vote accepted

Untested. But try: <?php echo apply_filters( 'the_content', limit_text( get_the_content(), 15 ) ); ?>

15 is for char limit or word limit? If words, using strlen is incorrect.

Try using WP wp_trim_words() instead.

share|improve this answer
if words its about 15 words, char i want to cut it at about 65 chars i test the code above, and the page become blank page. – Dzul Jan 4 '12 at 7:33
Trims text to 15 words: <?php echo apply_filters( 'the_content', wp_trim_words( get_the_content(), 15, '&hellip;' ) ); ?>. – zeo Jan 4 '12 at 7:44
It works now! Thanks! Now i need to make it included in the stylesheet, it just look unformatted now. Thanks for the help! – Dzul Jan 4 '12 at 7:51
Not related. But don't forget to reset your query. – zeo Jan 4 '12 at 8:02

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.