Support » Plugins » displaying the first n lines of a post

  • I’m setting up my Search Results page and I don’t want the full content of a post being displayed. An excerpt only.

    For example,

    date – Title

    date – Title

    I’m putting wordpress together for a client, so there is no guarantee that an “excerpt” will be entered for every post.

    Basically I check if an excerpt exists. If it does, print it, if it doesn’t print out the first ‘n’ lines of the post.

    My question is, is there a way to print out the first ‘n’ lines of a post? Either a wordpress solution or a random php solution would be fine.

    Note, using substr(string,0,[max characters]) isn’t a good solution because what if the post starts off with a list? It could end up being many lines before it reaches ‘max characters’.

    Hope I made sense. Thanks in advance.

Viewing 6 replies - 1 through 6 (of 6 total)
  • Mark (podz)


    Support Maven

    Look at The Loop – it contains the_content. If you replace that with the_excerpt, that should do what you wish.

    Mark (podz)


    Support Maven

    And you could specify the characters elsewhere – or in the case of certain posts, teach the client to use the ‘more’ quicktag ?

    Thanks for your reply, but as I stated in my post, I know about “the_excerpt” function, but it becomes useless of the person does not enter an excerpt. Yes, I could teach the client about <!–more–> and excerpt, and I will, but what if excerpt is not entered? I’d like some kind of fallback.

    Actually to use “the_excerpt” you don’t have to enter one. If you do, WP will use whatever you entered there, but if you don’t enter anything it will take automatically the first X number of words (I do not remember the exact data) and display it instead of the whole content.

    You are correct moshu. I just read that on the wordpress codex wiki.

    Anyway, I got some type of solution for extracting the first ‘n’ lines of a post.

    $c=0; //loop counter
    $output=”; //trimmed content

    //get the excerpt/content
    $tmp = ob_get_contents();
    //process the string
    $tmp = substr($tmp,0,$MAX_CHARS);

    $lines = explode(“\n”,$tmp);
    foreach($lines as $line) {
    if($c==$MAX_LINES) break;
    $output .= $line;

    echo $output;

    The only thing I couldn’t figure out how to do was append “…” to the end, so you’d have something like; “This is a very long string which…”

    The function “the_excerpt()” returns the html and I’m not sure how to go about finding the last html tag, then inserting “…” before the tag. Of course you have to watch out for <br /> too. If that exists prior to the last tag, it needs removal or “…” will be on the next line.

    Anyway, maybe someone will find the code useful.

    jbjaz, you should be able to avoid the small ob_* overhead by using get_the_excerpt(), which returns, but doesn’t display, the excerpt text. In fact, the_excerpt() is merely echoing it.

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘displaying the first n lines of a post’ is closed to new replies.