• I’m implementing jquery tabs and I need to number the IDs of #tab and src=”#tab” so that they match-up. I’m trying to use <?php echo $wp_query->post_count+1; ?> but on the second loop it keeps counting rather than starting over.

    Here’s what I’m trying to do:

    <ul class="tabs">
    <?php rewind_posts(); ?>
    <?php $loop = new WP_Query( array( 'post_type' => 'about sections', 'order' => 'asc', 'offset' => '1') ); ?>
    <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
    <li><a href="#tab<?php $my_post_count++; echo $my_post_count; ?>"><?php the_title(); ?></a></li>
    <?php endwhile; ?>
    </ul>
    <div class="tab_container">
    <?php rewind_posts(); ?>
    <?php $loop = new WP_Query( array( 'post_type' => 'about sections', 'order' => 'asc', 'offset' => '1') ); ?>
    <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
    <div id="tab<?php $my_post_count++; echo $my_post_count; ?>" class="tab_content">
    <?php the_content(); ?>
    </div>
    <?php endwhile; ?>
    </div>

    Any ideas?

Viewing 2 replies - 1 through 2 (of 2 total)
  • Thread Starter drinkonlyscotch

    (@drinkonlyscotch)

    Okay, I just said screw it and used <?php the_ID(); ?>

    Kinda sloppy, but it works.

    You don’t need the first rewind, or the second WP_Query..

    rewind_posts() basically moves the current item of the query back to the beginning..

    So you only need to do the query once, do the loop, then rewind, and then iterate over it again..

    Eg.

    <?php
    $loop = new WP_Query;
    $loop->query( array( 'post_type' => 'about sections', 'order' => 'asc', 'offset' => '1') );
    ?>
    <?php if( $loop->have_posts() ) : $loop_count = 0; ?>
    
    	<ul class="tabs">
    
    		<?php while ( $loop->have_posts() ) : $loop->the_post(); $loop_count++; ?>
    
    		<li><a href="#tab-<?php echo $loop_count; ?>"><?php the_title(); ?></a></li>
    
    		<?php endwhile; ?>
    
    	</ul>
    	<div class="tab_container">
    
    		<?php
    		$loop_count = 0; // Reset counter ready for next loop
    		rewind_posts(); // rewind, so you can do the while loop again  ?>
    
    		<?php while ( $loop->have_posts() ) : $loop->the_post(); $loop_count++; ?>
    
    		<div id="tab-<?php echo $loop_count; ?>" class="tab_content">
    			<?php the_content(); ?>
    		</div>
    
    		<?php endwhile; ?>
    
    	</div>
    
    <?php endif; ?>

Viewing 2 replies - 1 through 2 (of 2 total)
  • The topic ‘Numbering IDs’ is closed to new replies.