0

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?

2
  • Try get_the_content() Jan 4, 2012 at 4:14
  • hmm not working :(
    – Dzul
    Jan 4, 2012 at 4:23

1 Answer 1

1

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.

4
  • 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, 2012 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, 2012 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, 2012 at 7:51
  • Not related. But don't forget to reset your query.
    – zeo
    Jan 4, 2012 at 8:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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