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; ?>
The function wp_reset_query() should help http://codex.wordpress.org/Function_Reference/wp_reset_query
Use
wp_reset_query()
after each one of your custom queries:You might want to also look at the
wp_reset_postdata()
function, which would potentially be a better fit for you.Update
Try this: