Question

I have a problem retreiving posts into a page. I try to get posts of a specific category but when the posts get loaded they get displayed 5 times. I allready tried to change the number of posts to view in the admin panel but this doesn't have effect on the output of the posts.

This is my code:

<div id="primary" class="content-area">
    <main id="main" class="site-main" role="main">

        <?php

        query_posts( array ( 'category_name' => 'nieuwsitem', 'posts_per_page' => 20 ) );

         while ( have_posts() ) : the_post(); ?>

        <?php $myposts = get_posts('');
              foreach($myposts as $post) :
              setup_postdata($post);
              ?>
                <div class="post-item">
                  <div class="thedate"><?php echo get_the_time('d/m/Y', $post->ID); ?></p></div>
                  <div class="post-info">
                    <h2 class="post-title">
                    <a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>">
                    <?php the_title(); ?>
                    </a>
                    </h2>
                  </div>
                  <div class="post-content">
                  <?php the_content(); ?>
                  </div>
                </div>
              <?php endforeach; wp_reset_postdata(); ?>

        <?php endwhile; // end of the loop. ?>

    </main><!-- #main -->
</div><!-- #primary -->

Thanks in advance!

Was it helpful?

Solution

You've created 2 loops. Removing the foreach part of your code should fix this:

<div id="primary" class="content-area">
<main id="main" class="site-main" role="main">

    <?php

    query_posts( array ( 'category_name' => 'nieuwsitem', 'posts_per_page' => 20 ) );

     while ( have_posts() ) : the_post(); ?>

            <div class="post-item">
              <div class="thedate"><?php echo get_the_time('d/m/Y', $post->ID); ?></p></div>
              <div class="post-info">
                <h2 class="post-title">
                <a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>">
                <?php the_title(); ?>
                </a>
                </h2>
              </div>
              <div class="post-content">
              <?php the_content(); ?>
              </div>
            </div>

    <?php endwhile; // end of the loop. ?>

</main><!-- #main -->

OTHER TIPS

This is the proper way to do it. You should avoid use of query_posts and get_posts in most situations, and WP_Query is far more appropriate here.

The reason you are getting several copies of each post is because you modify the main query to get posts from that category, then inside the Loop you get_posts from that query and display all of them via foreach, so you are outputting every post in that category for every post in that category due to the nested loops. get_posts is not for use in conjunction with the Loop.

This should do it for you:

<div id="primary" class="content-area">
    <main id="main" class="site-main" role="main">

        <?php

        $catquery = new WP_Query('category_name=nieuwsitem&posts_per_page=20');
        if ($catquery->have_posts()) :
        while ( $catquery->have_posts() ) : $catquery->the_post(); ?>

                <div class="post-item">
                  <div class="thedate"><?php echo get_the_time('d/m/Y', $post->ID); ?></p></div>
                  <div class="post-info">
                    <h2 class="post-title">
                    <a href="<?php the_permalink() ?>" title="<?php the_title_attribute(); ?>">
                    <?php the_title(); ?>
                    </a>
                    </h2>
                  </div>
                  <div class="post-content">
                  <?php the_content(); ?>
                  </div>
                </div>
        <?php endwhile; // end of the loop. 
        else :
            echo '<h3>No posts found.</h3>';
        endif; ?>

    </main><!-- #main -->
</div><!-- #primary -->

Try :

$args array ( 'category_name' => 'nieuwsitem', 'posts_per_page' => 20 ) ;

$myposts = get_posts( $args );

And remove :

while ( have_posts() ) : the_post(); ?>

endwhile;

I think you query your post with two query so every times you get a post with : while ( have_posts() ) : the_post();

You query the posts again with : $myposts = get_posts('');

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top