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 need a foreach loop inside "the loop"

This is my current code (index.php) :

<?php if (have_posts()) : while (have_posts()) : the_post(); ?> 
<div><a href="<?php the_permalink(); ?>"  title="<?php get_the_title(); ?>"> <?php the_title(); ?></a></div>
<div><a data-backdrop="true" data-controls-modal="modal-from-dom" href="#">Show tags</a></div>
<div id="modal-from-dom">Popup contents goes here (tags) </div>
<?php endwhile; ?>
<?php else : ?>
<?php endif; ?>

It lists post title and tags in the index php. I display all my tags in a pop up box. User should click "show tags" link to view the tags.

Popup doesn't work correctly when i have more than 1 post. Because i need a unique name for each popup.

I mean instead of "modal-from-dom" for all i need like "modal-from-dom-1,modal-from-dom-2,etc"

I tried the following code but its not working.

<?php if (have_posts()) : while (have_posts()) : the_post(); ?> 
<?php $modalcount = 1; ?> 
    <div><a href="<?php the_permalink(); ?>"  title="<?php get_the_title(); ?>"> <?php the_title(); ?></a></div>
    <div><a data-backdrop="true" data-controls-modal="modal-from-dom-<?php echo $modalcount; ?>" href="#">Show tags</a></div>
    <div id="modal-from-dom-<?php echo $modalcount; ?>">Popup contents goes here (tags) </div>
<?php $modalcount++; ?>
    <?php endwhile; ?>
    <?php else : ?>
    <?php endif; ?>

Can anyone help me?

share|improve this question
add comment

2 Answers 2

up vote 1 down vote accepted

Why not just use the_ID() which will always be unique and won't require any extra code to be used.

share|improve this answer
    
Wonderful solution. worked like a charm. Thankyou :) –  Giri Dec 28 '11 at 2:17
add comment

The reason it's not working is because you define modalcount inside the WordPress 'while' loop - put that before

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

and it should work perfectly.

Edit: so what you want is:

<?php $modalcount = 1; ?> 
<?php if (have_posts()) : while (have_posts()) : the_post(); ?> 
    <div><a href="<?php the_permalink(); ?>"  title="<?php get_the_title(); ?>"> <?php the_title(); ?></a></div>
    <div><a data-backdrop="true" data-controls-modal="modal-from-dom-<?php echo $modalcount; ?>" href="#">Show tags</a></div>
    <div id="modal-from-dom-<?php echo $modalcount; ?>">Popup contents goes here (tags) </div>
<?php $modalcount++; ?>
    <?php endwhile; ?>
    <?php else : ?>
    <?php endif; ?>
share|improve this answer
    
Hey thanks for the answer... –  Giri Dec 28 '11 at 2:18
add comment

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.