Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

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; ?>
share|improve this question
add comment

2 Answers

The function wp_reset_query() should help http://codex.wordpress.org/Function_Reference/wp_reset_query

share|improve this answer
add comment

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; ?>
share|improve this answer
    
i tried it both but it's not working?!?! nothing happens, the third loop still grabs posts from one of the first loops on that page! –  JochenSchmidt Oct 18 '12 at 7:39
    
Your third loop starts with <?php if (have_posts()) : ?> and doesn't seem to have any query to filter posts. So it is listing ALL posts. You need add a query_posts or any other filter if you want to exclude posts. –  mirage Oct 18 '12 at 7:49
    
@JochenSchmidt - please see my updated post. –  stealthyninja Oct 18 '12 at 7:50
    
ok, your code comes close to the solution but now EVERY post from EVERY loop has "<?php the_title(); ?>" from the third loop (the main page loop)!?!?! –  JochenSchmidt Oct 18 '12 at 8:09
    
Replace the_title() in your custom queries with $title = get_the_title( $my_post->ID ); echo $title;. –  stealthyninja Oct 18 '12 at 8:20
show 1 more comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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