Wordpress Hack the_content() display xx or 300 words only


6 Votes, Rating: 4.331 Star2 Stars3 Stars4 Stars5 Stars

Tags:

Many developer ask, how to display short description or 300 words on post, on listing page so can display many posts on single page, if user like to read that post, he/she go ahead and click on details link to read fully post.

for it you need to use substr like this as below

<?php echo substr(strip_tags($post->post_content), 0, 250);?>

you can change ‘250′ with any numbers of words you want to display on listing page. or any place

Share/Save/Bookmark

21 Responses to “Wordpress Hack the_content() display xx or 300 words only”

  1. Yash says:

    Great, thanks its really simple to do.. i was working to find a solution from a long time
    Yash

  2. RaiulBaztepo says:

    Hello!
    Very Interesting post! Thank you for such interesting resource!
    PS: Sorry for my bad english, I’v just started to learn this language ;)
    See you!
    Your, Raiul Baztepo

  3. Seb says:

    An other way to do it

    ‘, ‘]]>’, $content);
    echo substr(strip_tags($content), 0, 250).’…’ ;?>

  4. Seb says:

    An other way to do it

    $content = get_the_content($more_link_text, $stripteaser, $more_file);
    $content = apply_filters(’the_content’, $content);
    $content = str_replace(’]]>’, ‘]]>’, $content);
    echo substr(strip_tags($content), 0, 250).’…’ ;

  5. srinvas says:

    thanks

  6. ale_mar says:

    Hi! Is there a way to get only the first line of the post content?
    Thx in advance!

  7. SAN says:

    i guess, no untill you assign special value of that line..
    like

    1st line #
    #= let system know that 1st line end

  8. kristina says:

    this is great, only creates a bug where if you have an image with caption in your post, you will see what looks like vB tags for the caption in your excerpt. example:

    [caption id="attachment_116" align="alignnone" width="300" caption="this is a caption"][/caption]

  9. lefti says:

    wow
    thanks!
    helped me alot!

  10. mobil bekas says:

    can help me how to hack values the_content() from wordpress command?

  11. WP User says:

    This gives you the first X characters, not words…

  12. Srijan says:

    Very Nice Trick :-)

  13. Angkor says:

    It’s a good resource. But I don’t know how to use it and how to apply it in my theme! :) Thanks if have any help! :D

  14. SAN says:

    you need to edit your themes pages, replace the_content(); to < ?php echo substr(strip_tags($post->post_content), 0, 250);?>

  15. WavePHP says:

    WP User: This function will get $limit of words from $content or the get_the_content() method.
    Just from the top of my head, might need some fixing.

    <?php
    function get_word_limit( $limit = 10, $content = ” ) {
    if( empty( $content ) ) {
    $content = get_the_content();
    }

    $segments = explode(’ ‘, strip_tags( html_entity_decode( $content ) ) );
    $return = ”;

    for( $i = 0; $i < $limit; $i++ ) {
    $return .= $segments[ $i ];
    }

    unset( $segments );

    return $return;
    }

    SAN: Using will function better as it removes tags, decodes any HTML enitities like  .

  16. bluepicaso says:

    doesnt seem to work

    $cntnt = the_content(); substr(strip_tags($cntnt, “img”), 0, 200);

    Its just not working at all

  17. Andy says:

    @bluepicaso,

    You cannot use $cntnt=the_content(). Instead of the_content(), use get_the_content() or $post->post_content. the_content() always prints the content inside.

    Best.

  18. chai says:

    Thanks I have develop on my site.

  19. Ronjon says:

    Thanks this is a great help.

  20. pam says:

    the image is not showing. any help?

  21. mang anyunk says:

    O my lord, it’s so simple… Thank you!!!

Leave a Reply