WordPress.org

Ready to get started?Download WordPress

Forums

[resolved] Individual id's for each iteration (5 posts)

  1. NinjaPirateSpy
    Member
    Posted 2 years ago #

    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.

  2. alchymyth
    The Sweeper & Moderator
    Posted 2 years ago #

    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)

  3. NinjaPirateSpy
    Member
    Posted 2 years ago #

    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!

  4. alchymyth
    The Sweeper & Moderator
    Posted 2 years ago #

    you are welcome ;-)
    which suggestion did you adopt in the end?

  5. NinjaPirateSpy
    Member
    Posted 2 years ago #

    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!

Topic Closed

This topic has been closed to new replies.

About this Topic