• On my blog I’m attempting to run three different areas containing content:

    First, the loop, which I’d like to narrow to one (featured) category. This I’ve been able to do with the following, placed right before the loop:

    <?php query_posts ($query_string . '&cat=4'); ?>

    The second and third contain similar code, on which queries only items from one category, the other which queries items from all categories but one.

    <ul>
    <?php
     global $post;
     $myposts = get_posts('numberposts=5&offset=1&category=3');
     foreach($myposts as $post) :
     ?>
    <li><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
     &nbsp;||&nbsp;<span class="date"><?php the_time('F j, Y'); ?></span>
     <div class="entry"></div><?php the_excerpt(); ?>
    </li>
     <?php endforeach; ?>
    </ul>

    Everything seems to work fine, except the_excerpt, which displays an excerpt of the one post over and over again. (The post is the latest chronological article that is NOT of the category defined in the query_posts call right before the main loop.) Titles and links display and act just fine.

    So the question: how do I get the_excerpt to display an excerpt of the appropriate story (and not repeat)?

    Thanks in advance for any help.

Viewing 2 replies - 1 through 2 (of 2 total)
  • from the codex:

    <?php
     $lastposts = get_posts('numberposts=3');
     foreach($lastposts as $post) :
        setup_postdata($post);
     ?>
     <h2><a href="<?php the_permalink(); ?>" id="post-<?php the_ID(); ?>"><?php the_title(); ?></a></h2>
     <?php the_content(); ?>
     <?php endforeach; ?>

    Try adding the setup_postdata($post); into your code in the corresponding place in the code

    Thread Starter gnorb

    (@gnorb)

    It works, thank you! I’d been looking for the answer to this for days.

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Problem with the_exerpt’ is closed to new replies.