Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm working on WordPress where I have following code to get posts within loop.

        <?php
                $posts = $woo_options['woo_latest_entries'];
                query_posts('post_type=post&category_name=company');
                if ( have_posts() ) : while ( have_posts() ) : the_post(); $count++;

        ?>

        /// Post Content Goes Here //

        <?php endwhile; endif; ?>

Which Output posts inside Loops something like this...

Post Goes Here ....

Other Post Goes Here ....

Another Post Goes Here ....
.....

What I want is to print current posts index number within loop. Example

 1. Post Goes Here ....

 2. Other Post Goes Here ....

 3. Another Post Goes Here ....
 .....

How Can I Achieve This ? Thanks.

EDIT

Ohh ! I can do it this way ..

<?php 
echo $wp_query->current_post +1; 
?>

Is there any other / Better way ?

share|improve this question

4 Answers 4

for some reason, you already have a counter variable in the loop; if this is not used for other purposes, simply echo it:

<?php echo $count.'.'; ?> /// Post Content Goes Here // 
share|improve this answer

Actually I want to assign ID's as per post index !

Here's your code that I modified.

<?php

global $wp_query;

$posts = $woo_options['woo_latest_entries'];
query_posts('post_type=post&category_name=company');

if ( have_posts() ) : while ( have_posts() ) : the_post();  $count++;
    $index = $wp_query->current_post + 1;

?>
    <div id="my_post_<?php echo $index; ?>">

        <!-- Post Content Goes Here -->

    </div>

<?php endwhile; endif; ?>
share|improve this answer

If it's just an esthetic thing and you don't need to use the count variable for further coding, you can just wrap your posts in an ol tag :

<?php if ( have_posts() ) : ?>

    <ol>

        <?php while ( have_posts() ) : the_post(); ?>

            <li> <!-- Post Content Goes Here --> </li>

        <?php endwhile; ?>

    </ol>

<?php endif; ?>
share|improve this answer
    
Actually I want to assign ID's as per post index ! –  MANnDAaR Jun 23 '11 at 9:09
    
@MANnDAaR, that's exactly what it does. If your loop has 10 posts, you'd see an ordered list, numbered from 1 to 10. (see example here) –  mike23 Jun 23 '11 at 9:15

Hi I bumped onto this thread, wondering how to do that too. Found out it's bloody easy. In the main template file, for example index.php, declare a variable $post_idx before the loop, and within the loop increment that var. Like this:

<?php $post_idx = 0; while ( have_posts() ) : the_post(); ?>
  <?php
    get_template_part( 'content', get_post_format() );
    $post_idx++;
  ?>
<?php endwhile; ?>

Then in your content template (for example content.php) that is executed everytime within the loop, just make $post_idx global and then use it to your needs:

global $post_idx;
print "<p>{$post_idx}</p>";

That's it!

share|improve this answer
    
You should prefix global variables to avoid naming collisions. –  toscho Oct 10 '12 at 16:43

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.