WordPress.org

Ready to get started?Download WordPress

Forums

displaying the first n lines of a post (7 posts)

  1. jbjaz
    Member
    Posted 9 years ago #

    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
    line1
    line2
    line3

    date - Title
    line1
    line2
    line3

    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.

  2. Mark (podz)
    Support Maven
    Posted 9 years ago #

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

  3. Mark (podz)
    Support Maven
    Posted 9 years ago #

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

  4. jbjaz
    Member
    Posted 9 years ago #

    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.

  5. moshu
    Member
    Posted 9 years ago #

    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.

  6. jbjaz
    Member
    Posted 9 years ago #

    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.

  7. Kafkaesqui

    Posted 9 years ago #

    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.

Topic Closed

This topic has been closed to new replies.

About this Topic