Forum Replies Created

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter jbjaz

    (@jbjaz)

    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.

    $MAX_CHARS=200;
    $MAX_LINES=3;
    $c=0; //loop counter
    $output=”; //trimmed content

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

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

    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.

    Thread Starter jbjaz

    (@jbjaz)

    podz:
    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.

Viewing 2 replies - 1 through 2 (of 2 total)