Support » Fixing WordPress » The Loop problems

  • Resolved NBZ


    Hi all.

    I am trying to add a loop before the loop which shows the main page content. The problem I am having is that apart from the homepage (which works), I am getting this error instead of the contents: “Sorry, no posts matched your criteria.”

    The loop looks fine to me… am I missing something obvious? (on most pages I have four different loops with no errors. This fifth one seems to cause all the errors. I wrote/modified the others a year or two ago, so it may be something obvious I have since forgotten…)

    The loop I added was:

    <div class="title">Affiliates</div>
    <div class="rtext">
    $AffiliateCount = 0;
    $Affiliates = 3;
    <?php query_posts('cat=60'); ?>
          <?php if (have_posts()) : while (have_posts()) : the_post(); ?>
          <?php if ($AffiliateCount == $Affiliates ) {
    } else { ?>
    <div class="affiliate">
    	<?php the_content();
    			 $AffiliateCount = $AffiliateCount + 1; ?>
    <?php	} ?>
    	<?php endwhile; else: ?>
    	<div class="noaffiliate">Affiliates will go here.</div>
    	<?php endif; ?>


Viewing 5 replies - 1 through 5 (of 5 total)
  • query_posts() should really be used only to affect the main (and hopefully only) loop on a page because it has a habit of banging the $posts object around in ways people are not prepared for.

    Try modifying your loop above like so and see if it works for you:

    <?php $aff_posts = new WP_Query('cat=60'); ?>
          <?php if ($aff_posts->have_posts()) : while ($aff_posts->have_posts()) : $aff_posts->the_post(); ?>

    Thanks, that worked.

    It was something I really did not know. Is this documented in the Codex?

    NBZ — there is this in Codex:

    If there are other places appropriate for that please suggest or even better, anyone can edit Codex.

    Sorry, my mistake. I even looked at that page. Blame my stupidness.

    Josh Fialkoff



    I am trying to use the loop to show excerpts from sub pages within a directory.

    The page I am trying to do this on is:

    I would like to have the excerpt for each page under each bullet.

    Here is the code in the template which draws the link.

    I think I need to use the <?php the_excerpt(); ?> tag, but I am not sure how (or even if that is correct). 😉

    Thanks for your help!


    <?php the_content('<p class="serif">More &raquo;</p>'); ?>
      wp_list_pages('title_li=&child_of='.$post->ID.'&date_format=$date_format'); ?>
    	<?php wp_link_pages(array('before' => '<p><strong>Pages:</strong> ', 'after' => '</p>', 'next_or_number' => 'number')); ?>
Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘The Loop problems’ is closed to new replies.