How can i display post with 30 words only in wordpress

I am trying to show posts in my home page of my theme where i want to restrict the post content to be displayed as only 20 words. I can use get_content to get the content value but how can i restrict it to show only 20 words. Is there something i can do in my theme to have this restriction on displaying only in the home page

Thanks
Prady

Related posts

Leave a Reply

2 comments

  1. what you want to do is conditionally use excerpts. First you have to edit the excerpt length, Add this to your functions.php file:

    function new_excerpt_length($length) {
    return 20;
    }
    add_filter('excerpt_length', 'new_excerpt_length');
    

    If you want to change the text that it appends on the end, or remove it add this directly under the above:

    function new_excerpt_more($more) {
    global $post;
    return ''; // this will remove it, '...' is pretty common to signify more content though.
    }
    add_filter('excerpt_more', 'new_excerpt_more');
    

    Then, if your homepage is your blog page, add the following to index.php, otherwise add it to page.php or which ever template you’re using for the page in question, instead of <?php the_content(); ?>:

    <?php if (is_page('home')) { the_excerpt(); } else { the_content(); } ?>
    

    This should do the trick and uses WordPress’s built-it functionality.

    I suggest trawling the WordPress codex, here, it’s full of helpful goodies in regards to content manipulation.