WordPress.org

Ready to get started?Download WordPress

Forums

Bug in handling of the_excerpt? (3 posts)

  1. adamrice
    Member
    Posted 6 years ago #

    I'm polishing up a new theme that shows a single post in full, followed by five excerpts. My code goes something like this

    query_posts (posts_per_page = 1)
    if have_posts
    while have_posts the_post
    -- do single-post stuff -
    
    global $post
    $myposts = get_posts('numberposts=5&offset=1')
    foreach($myposts as $post) :
    
    -- do recent-excerpt stuff
    endforeach
    endwhile
    
    else
    -- 404 stuff
    endif

    So here's the weird part: if I've explicitly written excerpt text for the excerpted entries, they appear correctly. If I do not, then I get clipped text from the most recent entry (that is, the one being shown in full).

    There may be a better way to organize all this, and I'd appreciate any guidance on restructuring the whole latest-post/excerpted-post stuff.

  2. David Yeiser
    Member
    Posted 6 years ago #

    I would recommend organizing it this way:

    <?php
    $posts = get_posts("numberposts=1");
    foreach($posts as $post) : setup_postdata($post);
    ?>
    
    FORMAT SINGLE POST IN FULL
    (Use the_title(), the_content(), etc. since we used setup_postdata().)
    
    <?php
    endforeach;
    ?>
    
    <?php
    $posts = get_posts("numberposts=5&offset=1");
    foreach($posts as $post) : setup_postdata($post);
    ?>
    
    FORMAT OTHER FIVE EXCERPT POSTS
    (Use the_title(), the_excerpt(), etc. since we used setup_postdata().)
    
    <?php
    endforeach;
    ?>

    With my WordPress install (2.1.3) the database doesn't produce any errors if there aren't enough posts to fulfill the get_posts() query. If it requests 5 and there's only 3, then it just shows those three.

    Let me know if this works for you.

  3. adamrice
    Member
    Posted 6 years ago #

    That was quick, thank you. Seems to work fine.

    One potential problem is that there's no fallback when there are zero posts. Of course, that's an edge case, and I can live with this.

    fwiw, the blog is http://8stars.org/a

Topic Closed

This topic has been closed to new replies.

About this Topic