Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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>' ;
        ?>
    <?php 
        $query = new WP_Query();
        $query->query('post_type=testimonial&posts_per_page='.$count);
        ?>
        <?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>
            </div>
        <?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
add comment

1 Answer

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
add comment

Your Answer

 
discard

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.