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.

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>";    
?>
share|improve this question
    
Any progress on this question? You got three (high level) answers provided. –  kaiser Nov 26 '12 at 2:59
add comment

2 Answers 2

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;
}
share|improve this answer
1  
Just found wp_trim_words/*_excerpt(). Kool thing to note: It also takes care of languages where 1 character = sign like Chinese. –  kaiser Oct 15 '12 at 1:30
1  
how about if there is a html inside a post content? would it be also counted into the word limit? i just dont want to include the html tags on it and remain it on side so that whenever there is a html, the pure contents is only the will be catch by the function which is to limit the word count. –  Juliver Galleto Oct 15 '12 at 22:03
1  
The wp_trim_words removes all HTML tags. See source here –  Rilwis Oct 16 '12 at 1:01
add comment

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.
share|improve this answer
    
how about if there is a html inside a post content? would it be also counted into the word limit? i just dont want to include the html tags on it and remain it on side so that whenever there is a html, the pure contents is only the will be catch by the function which is to limit the word count. –  Juliver Galleto Oct 15 '12 at 22:02
    
No, the_excerpt has no HTML tags. Hint: Always try this before reporting back. Try and error is easier and discussing and guessing :) Thanks. –  kaiser Oct 15 '12 at 22:04
add comment

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.