• The “Note for Multiple Posts in the First Category” in the this page:
    https://codex.www.ads-software.com/The_Loop
    is clearly wrong.

    Can anyone tell me what it should be?

    If showposts=2 or more, you will need to alter the code a bit. The variable $do_not_duplicate needs to be changed into an array as opposed to a single value. Otherwise, the first loop will finish and the variable $do_not_duplicate will equal only the id of the latest post. This will result in duplicated posts in the second loop. To fix the problem replace
    
    <?php if (have_posts()) : while (have_posts()) : the_post(); if( $post->ID == $do_not_duplicate ) continue; update_post_caches($posts); ?>
    
    with
    
    <?php if (have_posts()) : while (have_posts()) : the_post();
      if( $post->ID == $do_not_duplicate[] ) continue; update_post_caches($posts); ?>
    
    This changes $do_not_duplicate into an array. Then replace
    
    <?php if (have_posts()) : while (have_posts()) : the_post(); if( $post->ID == $do_not_duplicate ) continue; update_post_caches($posts); ?>
    
    with
    
    <?php if (have_posts()) : while (have_posts()) : the_post(); if($post->ID == $do_not_duplicate[0] || $post->ID == $do_not_duplicate[1] || $post->ID == $do_not_duplicate[2]) continue; update_post_caches($posts); ?>
    
    (Or use php's in_array().) Where you continue the pattern for whatever showposts is set equal to (2 in this case).
Viewing 6 replies - 1 through 6 (of 6 total)
  • Actually, it’s not. I’ve used that exact piece of code several times, and it works just fine.

    Why don’t you tell us *why* you think it’s wrong?

    Thread Starter bonusball

    (@bonusball)

    It’s totally possible that I’m not understanding it. But the Codex says you need to turn the var $do_not_duplicate into an array. But I don’t think it’s doing that.

    The instructions also tell you to change this line twice:

    <?php if (have_posts()) : while (have_posts()) : the_post(); if( $post->ID == $do_not_duplicate ) continue; update_post_caches($posts); ?>

    That can’t be right. Why would I change it twice? Once should be enough.

    So when I’m finished, the original code snippet looks like this, if I follow the instructions in the Codex:

    <?php $my_query = new WP_Query('category_name=featured&amp;showposts=2');
    while ($my_query->have_posts()) : $my_query->the_post();
    $do_not_duplicate = $post->ID;?>
    <!-- Do stuff... -->
    <?php endwhile; ?>
    <!-- Do other stuff... -->
    <?php if (have_posts()) : while (have_posts()) : the_post(); if($post->ID == $do_not_duplicate[0] || $post->ID == $do_not_duplicate[1] || $post->ID == $do_not_duplicate[2]) continue; update_post_caches($posts); ?>
    <!-- Do stuff... -->
    <?php endwhile; endif; ?>

    That looks wrong, and in fact doesn’t work.

    Am I missing something? I’m a little shaky on arrays.

    Thread Starter bonusball

    (@bonusball)

    The Codex is def. wrong.

    The end result should be:

    <?php $my_query = new WP_Query('category_name=featured&amp;showposts=2');
    while ($my_query->have_posts()) : $my_query->the_post();
    $do_not_duplicate[] = $post->ID ?>
    <!-- Do stuff... -->
    <?php endwhile; ?>
    <!-- Do other stuff... -->
    <?php if (have_posts()) : while (have_posts()) : the_post(); if($post->ID == $do_not_duplicate[0] || $post->ID == $do_not_duplicate[1] || $post->ID == $do_not_duplicate[2]) continue; update_post_caches($posts); ?>
    <!-- Do stuff... -->
    <?php endwhile; endif; ?>

    and in the instructions, the first step should go like this:

    replace 
    
     <?php $my_query = new WP_Query('category_name=featured&amp;showposts=1');
      while ($my_query->have_posts()) : $my_query->the_post();
      $do_not_duplicate = $post->ID;?>
    
    with
    
    <?php $my_query = new WP_Query('category_name=featured&amp;showposts=2');
    while ($my_query->have_posts()) : $my_query->the_post();
    $do_not_duplicate[] = $post->ID ?>

    doodlebee if you used that exact code several times, you better go check your blogs again, as that code is busted.

    Steve

    (@stevejohnson)

    You’re right, Bonusball.

    So edit the Codex. It’s a wiki. Sign up for an account and edit away.

    Personally, I would use
    if ( in_array( $do_not_duplicate, $post->ID) ) continue;
    instead of all of those ors ( || )…

    There really is an issue with this kind of code, though. If you have your options set to display 5 posts and you ‘continue’ past 3 of them in your main loop (because they’re in the $do_not_duplicate array), you’ll only see two posts – because the query doesn’t reset.

    Thread Starter bonusball

    (@bonusball)

    Well, what do you know, I didn’t know it was a wiki.

    Although if I’m logged in as Bonusball, I shouldn’t need to make another username for the wiki!

    Regardless, I fixed it. and I used your suggestion stevejohnson, although I fixed your code, too. It should have been

    if (in_array($post->ID, $do_not_duplicate)) continue;

    And I now what you’re saying about the problem, but that doesn’t happen.

    Who knows why?

    I’ve done my duty, off to dinner!

    I’m new to php, so the syntax is still tricky to me. I can’t seem to get this to work. Here’s my code:

    <?php query_posts('category_name=featured');
     if ( have_posts() ) : while ( have_posts() ) : the_post();
     $do_not_duplicate = $post->ID;
     $custom_field_date = get_post_meta($post->ID, 'project-date', true);
     $custom_field_disciplines = get_post_meta($post->ID, 'disciplines', true);?>
    
    		<!--do stuff-->
    
    <?php endwhile;
    wp_reset_query();
    ?>		
    
    <?php query_posts('category_name=winners');
     while ( have_posts() ) : the_post();
     $do_not_duplicate = $post->ID;
     $custom_field_date = get_post_meta($post->ID, 'project-date', true);
     $custom_field_disciplines = get_post_meta($post->ID, 'disciplines', true);?>
    
    		<!--do stuff-->
    
    <?php endwhile;
    wp_reset_query();
    ?>
    
    <?php query_posts('category_name=portfolio&showposts=50');
     while ( have_posts() ) : the_post();
     $do_not_duplicate = $post->ID;
     $custom_field_date = get_post_meta($post->ID, 'project-date', true);
     $custom_field_disciplines = get_post_meta($post->ID, 'disciplines', true);?>
    
    	<!--do stuff-->
    
    <?php endwhile; else: ?>
    
    	<p>Sorry, no posts matched your criteria.</p>
    
    <?php endif; ?>

    What am I doing wrong?

Viewing 6 replies - 1 through 6 (of 6 total)
  • The topic ‘Mistake in Codex page?’ is closed to new replies.