for loop counter $i=1;$i<=3;$i++ in wordpress is creating duplicates

Posted on 2014-08-15
Last Modified: 2014-09-15
I am using the below php code to add a class to each post from a specific category. The counter adds the number to class but each of the is adding 3 times. here is my mark up
	 <ul class="ul">	 
	 <?php query_posts('showposts=3&cat=3&orderby=rand'); if (have_posts()) : while (have_posts()) : the_post(); ?>
	 <?php for ( $i=1; $i<=3; $i++ ) { ?>
	 <li class="li<?php echo $i ?>"><a href="<?php $postid = $wp_query->post->ID; echo get_post_meta($postid, '_wpb_nsl_text', true);  ?>" title="<?php the_title_attribute(); ?>"><?php print $post->post_title; ?></a>
	 <a href="<?php $postid = $wp_query->post->ID; echo get_post_meta($postid, '_wpb_nsl_text', true);  ?>"><?php the_post_thumbnail('blog-post-image'); ?></a>
		<?php } ?>
	<?php endwhile; else : endif; ?>
	<?php wp_reset_query(); ?>

Open in new window

The result is like below and as you can see its creating 3 li's for each post instead of only one
<ul class="ul>
 <li class="li1">post 1 </li>
 <li class="li2">post 1 </li>
 <li class="li3">post 1 </li>
 <li class="li1">post 2 </li>
 <li class="li2">post 2 </li>
 <li class="li3">post 2 </li>
 <li class="li1">post 3 </li>
 <li class="li2">post 3 </li>
 <li class="li3">post 3 </li>

Open in new window

What I am looking is
<ul class="ul>
 <li class="li1">post 1 </li>
 <li class="li2">post 2 </li>
 <li class="li3">post 3 </li>

Open in new window

What am I missing?
Question by:niceoneishere
    LVL 38

    Accepted Solution

    You have a for loop inside of a while loop. The while loop runs three times because you requested three posts. You have set the for loop to run three times also. 3 x 3 = 9

    You want something more along these lines (not tested):
     <ul class="ul">
    // The Query
    query_posts( 'showposts=3&cat=3&orderby=rand' );
    // The Loop
    $i = 1;
    while ( have_posts() ) : the_post();
        $postid = $wp_query->post->ID;
        $postmeta = get_post_meta($postid, '_wpb_nsl_text', true);
        echo '<li class="li' . $i . '"><a href="' .  $postmeta  .  '" title="' . the_title_attribute() . '">' .  $post->post_title . '</a>
    	 <a href="' . $postmeta . '">' . the_post_thumbnail('blog-post-image') . '</a>
    // Reset Query

    Open in new window

    LVL 1

    Author Closing Comment

    Thanks :)

    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    Part of the Global Positioning System A geocode ( is the major subset of a GPS coordinate (, the other parts being the altitude and t…
    In order to have all security and back ups taken care of, WordPress users can sign up for services with WP Engine.
    This video teaches users how to migrate an existing Wordpress website to a new domain.
    The viewer will learn how to dynamically set the form action using jQuery.

    733 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now