[resolved] How to number each output with a link? (3 posts)

  1. christopherburton
    Posted 3 years ago #

    Overall what I'm trying to do is list the number of attachments (1, 2, etc) for each post on a page. When you click on a number, it will change the image. Pretty much like a slideshow. How would I go about adding the numbers in my loop or could this be better solved via jQuery?

    Code (so far):

    		if( function_exists( 'attachments_get_attachments' ) )
    			$attachments = attachments_get_attachments();
    			$total_attachments = count( $attachments );
    		if( $total_attachments ) : ?>
    		<ul class="process">
    			<?php for( $i=0; $i<$total_attachments; $i++ ) : ?>
    			<li><a href="<?php echo $attachments[$i]['location']; ?>"></a></li>
    			<?php endfor; ?>
    			<?php endif; ?>
    	<?php } ?>

    Desired Output (in view source HTML):

    <li><a href="http://domain.com/path/to/image1.png">1</a></li>
    <li><a href="http://domain.com/path/to/image2.png">2</a></li>
    <li><a href="http://domain.com/path/to/image3.png">3</a></li>
  2. Michael
    Forum Moderator
    Posted 3 years ago #

    you already have the counter variable $i in the for() loop; echo it;

    <li><a href="<?php echo $attachments[$i]['location']; ?>"><?php echo $i+1; ?></a></li>
  3. christopherburton
    Posted 3 years ago #

    Wow. It was that simple. Thanks, @alchymyth. I appreciate it.

Topic Closed

This topic has been closed to new replies.

About this Topic