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 have been using this code for a long time :

$temp_arr_content = explode(
    " ",
    substr(
        strip_tags(
            get_the_content()
        ),
        0,
        720
    )
);
$temp_arr_content[count( $temp_arr_content ) - 1] = "";
$display_arr_content = implode( " ", $temp_arr_content );
echo $display_arr_content;
if( strlen( strip_tags( get_the_content() ) ) > 0 )
    echo "...";

To limit my content to 720 characters and strip html from it
However, today I used it again in my new theme but I noticed that it also outputs the HTML codes (the HTML code is not working, but shows up in the content) .

I have also used this code in my theme :

$excerpt = get_the_excerpt();
echo string_limit_words( $excerpt, 55 );

and this code on my Function.php :

function string_limit_words( $string, $word_limit ) {
    $words = explode( ' ', $string, ( $word_limit + 1 ) );
    if( count( $words ) > $word_limit )
        array_pop( $words );
    return implode( ' ', $words );
}

to limit words and strip HTML , however, when I change 55 to 150 the code does not work and it still shows 55 words.
can someone please help me resolve this issue ? thanks

share|improve this question

1 Answer 1

up vote 1 down vote accepted

First, I wouldn't modify the string/word length of the content. Semantically, you're dealing with an excerpt, so let's focus on that.

Second, to limit the number of words returned for the excerpt, simply filter excerpt_length:

<?php
function wpse52673_filter_excerpt_length( $length ) {
    // Return (integer) value for
    // word-length of excerpt
    return 150;
}
add_filter( 'excerpt_length', 'wpse52673_filter_excerpt_length' );
?>

The added benefit of using the_excerpt() is that, for auto-generated excerpts, no HTML is parsed. So, if you don't use user-defined excerpts, you're done.

However, if you do use user-generated excerpts, but still want to strip HTML tags, simply remove any or all of the following filters that are applied to the_excerpt:

add_filter( 'the_excerpt',     'wptexturize'      );
add_filter( 'the_excerpt',     'convert_smilies'  );
add_filter( 'the_excerpt',     'convert_chars'    );
add_filter( 'the_excerpt',     'wpautop'          );
add_filter( 'the_excerpt',     'shortcode_unautop');

To remove one, simply call, e.g.:

remove_filter( 'the_excerpt', 'wptexturize' );
share|improve this answer

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.