Page-loop is looping content from custom loops on the same site. How to avoid that?

i’ve made a custom home-page (with a template) and i’m using three loops on this site: two category-loops and on the bottom of the site the main loop for the page (the page content). the problem is: the first loops are fine but the third (the main page loop) is looping the content from one of the other two loops, not from the actual site!?!

i think this happens because the main page-loop is the last one on that page. how can i avoid that?

Read More

here’s my code:

the first loop:

<?php
$posts = get_posts('category_name=slider');
foreach($posts as $post) :
setup_postdata($post);
?>
    <div class="startslide" style="background-image:url(<?php $src = wp_get_attachment_image_src( get_post_thumbnail_id($post->ID), array( 670,320 ), false, '' ); echo $src[0]; ?>)">
        <h4><?php the_title(); ?></h4>
        <div class="subline"><?php the_content(); ?></div>
    </div>
    <?php endforeach; ?>

the second one:

<?php
$posts = get_posts('category_name=meilensteine&numberposts=3');
foreach($posts as $post) :
setup_postdata($post);
?>
            <div class="meilpost">
                <h3><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
              <p><?php echo excerpt(14); ?></p>
         </div>
         <?php endforeach; ?>

and the thir one (the main page loop):

<?php if (have_posts()) : ?>

    <?php while (have_posts()) : the_post(); ?>
        <h1><?php the_title(); ?></h1>
        <?php the_content(); ?>
        <?php endwhile; ?>
    <?php endif; ?>

Related posts

Leave a Reply

2 comments

  1. Use wp_reset_query() after each one of your custom queries:

    <?php endforeach; ?>
    <?php wp_reset_query(); ?>
    

    You might want to also look at the wp_reset_postdata() function, which would potentially be a better fit for you.

    Update
    Try this:

    <?php
    global $post;
    $tmp_post = $post;
    
    $my_posts = get_posts( 'category_name=slider' );
    foreach ( $my_posts as $my_post ) :
    setup_postdata( $my_post );
    ?>
        <div class="startslide" style="background-image:url(<?php $src = wp_get_attachment_image_src( get_post_thumbnail_id( $my_post->ID ), array( 670,320 ), false, '' ); echo $src[0]; ?>)">
            <h4><?php the_title(); ?></h4>
            <div class="subline"><?php the_content(); ?></div>
        </div>
    <?php endforeach; ?>
    <?php $post = $tmp_post; ?>
    
    <?php
    $tmp_post = $post;
    
    $my_posts = get_posts( 'category_name=meilensteine&numberposts=3' );
    foreach ( $my_posts as $my_post ) :
    setup_postdata( $my_post );
    ?>
        <div class="meilpost">
            <h3><a href="<?php the_permalink() ?>" rel="bookmark" title="Permanent Link to <?php the_title_attribute(); ?>"><?php the_title(); ?></a></h3>
            <p><?php echo excerpt(14); ?></p>
         </div>
    <?php endforeach; ?>
    <?php $post = $tmp_post; ?>
    
    <?php if ( have_posts() ) : ?>
        <?php while ( have_posts() ) : the_post(); ?>
            <h1><?php the_title(); ?></h1>
            <?php the_content(); ?>
        <?php endwhile; ?>
    <?php endif; ?>