1

I have a problem with my loop.php, it keeps on repeating posts. I'm guessing this might be controlled outside of this loop.php as it happens no matter what I try. Could someone please advise me as to where it might be getting this information to repeat the loop 4 times.

Here is the current loop.php

<?php 
    $args = array ( 'category' => 4, 'posts_per_page' => 10);
    $myposts = get_posts( $args );
    foreach( $myposts as $post ) :  setup_postdata($post);

    ?>

    <!-- Begin Style Here -->

    <div id="post-<?php the_ID(); ?>" <?php post_class(); ?>>
        <div class="postmeta">
            <?php do_action('themezee_display_postmeta_index'); ?>
        </div>

        <h2 class="post-title"><a href="<?php the_permalink() ?>" rel="bookmark"><?php the_title(); ?></a></h2>

        <div class="entry">
            <?php the_post_thumbnail('thumbnail', array('class' => 'alignleft')); ?>
            <?php the_content('<span class="moretext">' . __('Read more', 'themezee_lang') . '</span>'); ?>
            <div class="clear"></div>
            <?php wp_link_pages(); ?>
        </div>

        <div class="postinfo">
            <?php do_action('themezee_display_postinfo_index'); ?>
        </div>
    </div>

    <!-- End Style Here -->

<?php endforeach; ?>

Thanks for any help with this

1
  • 1
    What do you mean by "repeating posts": are they all identical in this foreach loop? Do you have more than 4 posts assigned to this category?
    – birgire
    Apr 4, 2013 at 7:58

1 Answer 1

1

In order not to interfere with main wp_query I would recommend to use this hook, that I used on one of my websites.

//wp_query to buffer
$temp_query = clone $wp_query;

//Use main wp_query with attributes
$wp_query = new WP_Query( array( 'posts_per_page' => 10 ) ); 
if(have_posts()) : while(have_posts()) : the_post();
//your code with html and etc    
endwhile; endif;

//Get back to main wp_query
$wp_query = clone $temp_query;
3
  • Where do I insert this? into my code on the loop.php?
    – Bellatoppa
    Apr 4, 2013 at 17:04
  • My index.php I'm thinking is the issue and maybe why all the posts are repeated 4 times.
    – Bellatoppa
    Apr 4, 2013 at 17:11
  • This is most of my current index.php and I believe the problem <?php if (have_posts()) : while (have_posts()) : the_post();get_template_part( 'loop', 'index' ); endwhile; ?><?php if(function_exists('wp_pagenavi')) { // if PageNavi is activated ?><div class="more_posts"><?php wp_pagenavi(); ?></div><?php } else { // Otherwise, use traditional Navigation ?> <div class="more_posts"><span class="post_links"><?php next_posts_link(__('&laquo; Older Entries', 'themezee_lang')) ?> &nbsp; <?php previous_posts_link (__('Recent Entries &raquo;', 'themezee_lang')) ?></span> </div><?php }?><?php endif; ?>
    – Bellatoppa
    Apr 4, 2013 at 17:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.