• Resolved ramijames

    (@ramijames)


    Hi Guys and Gals,

    I’ve been using wordpress as a CMS for a site that I’m developing.

    I’ve hit a bit of a snag. I’m creating a custom archive page which I want to show only the header and an excerpt from each available ‘post’. This is going to show as a list of press releases. I’ve gotten everything working as I like.. but the posts show up three times instead of one. I mean something like this:

    Post 1
    Post 2
    Post 3
    Post 4
    Post 1
    Post 2
    Post 3
    Post 4
    Post 1
    Post 2
    Post 3
    Post 4

    Which is obviously not good.

    The code I’m using to generate the list is:

    <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
      <div class="archives">
        <ul>
    							       <?php
    global $post;
    $myposts = get_posts('offset=0');
    foreach($myposts as $post) :
    setup_postdata($post);
    ?>
    <li>
    <a href="<?php the_permalink(); ?>"><h2><?php the_title();?></h2></a>
    									<?php the_excerpt(); ?>
            </li>
          <?php endforeach; ?>
        </ul>
      </div>
    <?php endwhile; else: ?>
    
    <p>Sorry, no posts matched your criteria.</p>
    
    <?php endif; ?>

    It’s from the wp manual and I’m sure that I’ve messed up somewhere.

    Any help would be greatly appreciated.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter ramijames

    (@ramijames)

    I’ve found a solution which is much simpler.

    <?php query_posts('showposts=10000'); ?>
                             <div class="archives">
                                 <ul>
                                  <?php while (have_posts()) : the_post(); ?>
                                    <li>
                                    	<a href="<?php the_permalink(); ?>"><h2><?php the_title(); ?></h2></a>
                                    	<?php the_excerpt(); ?>
                                    </li>
                                  <?php endwhile; ?>
                                 </ul>
                             </div>

    I’m not really sure what the difference is, honestly.

    Your first example has two Loops.

    See also, The Loop in Action

    Thread Starter ramijames

    (@ramijames)

    Yes, I noticed that after studying it for a while.

    Thanks for the help.

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘get_posts() displaying multiple times in loop’ is closed to new replies.