php - Find position in while loop -
i have created while loop in wordpress outputs custom field , post data. want inside loop , check find if field matches highlight field , output position iteration.
my users have field in profile called 'schoolname' wan reference against.
$user_info = get_userdata(1); echo $user_info->schoolname; $i= 1; $loop = new wp_query( array( 'post_type' => 'schools', 'posts_per_page' => -1, 'orderby' => 'signups', 'order' => 'asc', ) ); while ( $loop->have_posts() ) : $loop->the_post(); echo '<tr> <td>' . $i++ . '</td> <td>' . get_the_title() . '</td> <td>' . $signups = get_field( "signups") . ' signups</td> </tr>'; endwhile; wp_reset_query();
to state of loop use foreach
<?php global $post; // required $posts = get_posts(); foreach($posts $state => $post) : setup_postdata($post); ... endforeach; ?>
you can replace while
part with foreach part. know iteration number use
$state`.
update
<?php $posts = get_posts(array( 'post_type' => 'schools', 'posts_per_page' => -1, 'orderby' => 'signups', 'order' => 'asc', )); ?> <?php if ($posts): ?> <?php foreach ($posts $key => $post): ?> <?php setup_postdata($post); ?> <tr> <td><?= $key; ?></td> <td><?= get_the_title(); ?></td> <td><?= get_field( "signups"); ?> signups</td> </tr> <?php endforeach; ?> <?php wp_reset_postdata(); ?> <?php endif; ?>
Comments
Post a Comment