[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now


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

Posted on 2014-08-15
Medium Priority
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

Tom Beck earned 2000 total points
ID: 40264957
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


Author Closing Comment

ID: 40323290
Thanks :)

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When it comes to security, close monitoring is a must. According to WhiteHat Security annual report, a substantial number of all web applications are vulnerable always. Monitis offers a new product - fully-featured Website security monitoring and pr…
Ready to get certified? Check out some courses that help you prepare for third-party exams.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
Suggested Courses
Course of the Month20 days, 7 hours left to enroll

872 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