WordPress.org

Support

Support » How-To and Troubleshooting » [Resolved] Individual id's for each iteration

[Resolved] Individual id's for each iteration

  • Hi,

    I am trying to get a unique Id for each iteration. I am able to get alternate id’s but would like to get a unique id for each so the 1st iteration would have an id=”first” 2nd id=”second” 3nd id=”third” and so on.

    here is the code I have so far which gets me the alternate id’s.

    http://pastebin.com/vBqCJJTA

    but mainly I am only dealing with these lines:

    <? $alt_post = 'alt-post';?>
    <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
    <?$alt_post = $alt_post == 'alt-post' ? 'first' : 'second';?>
    <?php echo '<li class = "four-count"> <p id="'. $alt_post.'">'?>

    Thanks for any help!
    -Mike

    Note:First post so I’m not sure how this code is going to show up.

Viewing 4 replies - 1 through 4 (of 4 total)
  • Michael

    @alchymyth

    Forum Moderator

    using ids with numbers would be easy;
    example:

    <?php $alt_post = 0; ?>
    <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
    <?php $alt_post++; ?>
    <?php echo '<li class = "four-count"> <p id="iteration-'. $alt_post.'">'; ?>

    if you want to use words, and as you haven’t stated any limits on your iterations, then you might need to add this function (works up to 99 iterations) to functions.php of your theme:
    http://pastebin.com/ZFUzdcSC

    example:

    <?php $alt_post = 0; ?>
    <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
    <?php $alt_post++; ?>
    <?php echo '<li class = "four-count"> <p id="'. convert_number($alt_post).'">'; ?>

    if ‘four-count’ indicates that the max number of iterations is 4, you could use an extension of your original code;
    example:

    <?php $alt_post = 'alt-post'; ?>
    <?php while ( $loop->have_posts() ) : $loop->the_post(); ?>
    <?php $alt_post = ($alt_post == 'alt-post') ? 'first' : (($alt_post == 'first') ? 'second' : (($alt_post == 'second') ? 'third' : (($alt_post == 'third') ? 'fourth' : 'first' ))); ?>
    <?php echo '<li class = "four-count"> <p id="'. $alt_post.'">'; ?>

    (not extensively tested)

    alchymyth,

    Thank you so much! Worked like a charm. I have been battling with this for weeks. I can’t tell you how much I appreciate your help.

    Cheers!

    Michael

    @alchymyth

    Forum Moderator

    you are welcome 😉
    which suggestion did you adopt in the end?

    The second one. The JQuery i’m using has incremental Id’s up to thirteenth (irrelevant) and scrolls to the “page” the custom post is on and then down 250px for each id. Couldn’t figure it out for the life of me. This helps me out on so many issues i was having besides this project too. Thanks again!

Viewing 4 replies - 1 through 4 (of 4 total)
  • The topic ‘[Resolved] Individual id's for each iteration’ is closed to new replies.
Skip to toolbar