limit the words in the post content and add read more link

As you can see into the code, the routine is to display the post which has an id of 266. Now all I want is to limit the words displayed in the post content of that post. Let’s say, I want to limit the words to a number of 300 and then add a read more link.

This is the code I got so far:

    $post_id = 266;
    echo "<div id='widgets-wrapper3'><div id='marginwidgets' style='overflow: auto; max-width: 100%; margin: 0 auto; border: none !important;'>";

    $queried_post = get_post($post_id); 
    echo "<div class='thewidgets'>";
    echo $queried_post->post_content;
    echo '</div>';

    echo "</div></div>";    
?>

Related posts

Leave a Reply

2 comments

  1. I always have the same problem with post excerpt, post content. There’re various hooks and functions for this purpose, like @kaiser pointed out. But sometimes they don’t do exactly what I want.

    Here’s my solution, I write my own function that take the post content, and truncate it into specified number of words:

    function wpse69204_excerpt( $num_words = 20, $ending = '...', $post_id = null )
    {
        global $post;
    
        // Truncate post content
        $current_post = $post_id ? get_post( $post_id ) : $post;
        $excerpt = strip_shortcodes( $current_post->post_content );
        $excerpt = wp_trim_words( $excerpt, $num_words, $ending );
    
        // Read more link
        $excerpt .= '<a href="' . get_permalink( $post ) . '" title="">Continue reading...</a>';
    
        return $excerpt;
    }
    
  2. There’re three filters that control the »more« link, depending on what function/Template Tag is in use. The bad thing is, that they’re intercepting each other. The good thing is, that you can simply modify the output of the filter using current_filter() to retrieve the name of the currently attached filter and modify the output.

    Then we got the 'excerpt_length'-filter to limit the excerpt length. This one doesn’t allow us to add a permalink, but it helps us in combination with the other filters. See the 2nd plugin.

    The permalink-more plugin

    This plugin adds the permalink to the content or excerpt – depending on what is displayed. It also resets the excerpt_more-filter to output nothing, so it’s not interfering with the other filters.

    <?php
    /** Plugin Name: (#69204) »kaiser« Adds a permalink to the excerpt & content */
    
    /**
     * Alters the display of the "more" link
     * 
     * @param  string $permalink
     * @param  string $text
     * @return string $html
     */
    function wpse69204_more_link( $output )
    {
        $html .= '<span class="post-more">&nbsp;';
        $html .= sprintf(
            '<a href="%s#more-%s" class="more-link" title="read more" >'
            ,get_permalink()
            ,get_the_ID()
        );
        $html .= '</a></span>';
    
        // Override 'excerpt_more'
        if ( 'excerpt_more' === current_filter() )
            return;
    
        // Strip the content for the `get_the_excerpt` filter.
        $output = wp_trim_words( $output, 300 );
    
        // Append for the excerpt
        if ( 'get_the_excerpt' === current_filter() )
            return $output.$html;
    
        // The permalink for the `the_content_more_link`-filter.
        return $html;
    }
    # "More" link for the content
    add_filter( 'the_content_more_link', 'wpse69204_more_link' );
    add_filter( 'get_the_excerpt', 'wpse69204_more_link' );
    add_filter( 'excerpt_more', 'wpse69204_more_link' );
    

    The excerpt-more length plugin

    If you just want to modify the lenght of the excerpt, you can use a much simpler filter setup. The following plugin does a very nifty job. It reduces the content (we’re in the loop and have post data to access) to 300 words. In the next step it counts the letters of each single word. Then it simply returns this (dynamically assigned) number.

    <?php
    /** Plugin Name: (#69204) »kaiser« Limit excerpt length by word count */
    
    function wpse69204_excerpt_length( $length )
    {
        $to_count = array_splice( get_the_content(), 300 );
        $i = 0;
        foreach ( $to_count as $word )
        {
            $i += strlen( $word );
        }
    
        return $i;
    }
    add_filter( 'excerpt_length', 'wpse69204_excerpt_length' );
    

    Notes

    1. Both plugins are »zero configuration«. Just upload, activate, done.
    2. You’ll have to use the_content() or the_excerpt() in your theme to make use of this plugins.