WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] get_posts() displaying multiple times in loop (4 posts)

  1. ramijames
    Member
    Posted 6 years ago #

    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.

  2. ramijames
    Member
    Posted 6 years ago #

    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.

  3. MichaelH
    Member
    Posted 6 years ago #

    Your first example has two Loops.

    See also, The Loop in Action

  4. ramijames
    Member
    Posted 6 years ago #

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

    Thanks for the help.

Topic Closed

This topic has been closed to new replies.

About this Topic