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've created a page with a list of posts from a specific category. I would like to enumerate this list like this:

  • lecture 1: post title 1
  • lecture 2: post title 2
  • lecture 3: post title 3
  • etc.

How can I do this?

Thanks

share|improve this question
    
So what you basically asking is how to list posts but prefix the titles with Lecure # (where # is a numeric value), right? Would it be safe to assume, the first in the list will naturally be number one, and the second number two, and so on... –  t31os Feb 18 '11 at 12:25
    
Yes, this is exactly it –  user3273 Feb 19 '11 at 9:46

3 Answers 3

I am not sure what your loop looks like from which you are adding the articles to your list. All you need do is to create a variable to hold your counter somewhere before the while statement in your loop. This could be something like

<?php $counter = 0; ?>

Note I am setting the counter to 0. Within the while statement you now enumerate this variable, this will keep on adding one to the counter for every time through the loop.

<?php $counter++ ;?>

Inside your loop you now use the variable to show the numbers you are interested in. This will give you a one on the first loop, two on the second etc.

Hope this helps.

share|improve this answer

I adapted my loop to be like the one proposed by RXN, so it is like this:

<ul>
<?php
global $post;
$args = array( 'category' => 3, 'offset'=> 1, 'numberposts' => -1, 'order' => asc, 'exclude' => '4307,4321');
$myposts = get_posts( $args );
foreach( $myposts as $post ) :  setup_postdata($post); ?>
<div>
    <li class="paginaIndex-temario"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li></div>
<?php endforeach; ?>
</ul>

and I introduced a list-style-type:decimal; in the stylesheed. Nevertheless I got a consecutive list of numbers for each post title everywhere but on Explorer (this give me a list of 1.,1.,1.,etc.) so I'm, at best, half way to success.

Any thoughts? I would like to try whith the <?php $counter = 0; ?> but I really don't know what to do with it.

Thanks

share|improve this answer
    
How about using ol instead of ul? –  wyrfel Feb 20 '11 at 10:50

Here is what I found in the Codex. Simply replace the category number with the one you are using. I modified it with the counter you requested.

<ul>
<?php
global $post;
$args = array('category' => 6, 'offset' => 1, 'numberposts' => -1, 'order' => asc, 'exclude' => '4307,4321');
$myposts = get_posts($args);
$counter = 0;
foreach ($myposts as $post) :  setup_postdata($post);
?>
    <div>
        <li class="paginaIndex-temario">Lesson # <?php echo $counter++;?> <a
                href="<?php the_permalink(); ?>"><?php the_title(); ?></a></li>
    </div>
<?php endforeach; ?>
</ul>

HTH

share|improve this answer
    
Hi RXN, I added the 'offset' to the code (everything else is exaclty like your code) but nothing happens. The numbers are not there. –  user3273 Feb 18 '11 at 9:36
    
I've been thinking that I did not explain myself correctly. I already have the list of posts, what I would like to do is to assign, in front of every post title a number progression such as lecture 1: title post 1; lecture 2: title post 2, etc. Is this possible? –  user3273 Feb 18 '11 at 10:45
    
Hi Pere, I edited my post with the counter. I tested this in my machine and it displayed "Lesson # 1 ..., Lesson #2...". I hope that helps. –  rexposadas Feb 20 '11 at 5:38

Your Answer

 
discard

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