Web Analytics Made Easy -
StatCounter Extra class in last of while loop? - CodingForum

Announcement

Collapse
No announcement yet.

Extra class in last of while loop?

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Extra class in last of while loop?

    Hey, using advanced custom fields for wordpress which has a repeater field function as follows. i'm interested in a way to add a special class to the last P tag in the array. Not sure how.

    PHP Code:
    <?php while(the_repeater_field('test')): ?> 
    <p><?php the_sub_field('test-sub'); ?></p> 
    <?php endwhile; ?>

  • #2
    This solution may seem a bit long-winded following the 3 lines of code above, but it is only because WordPress' functions offer little control for honest developers and seem to cater more toward designers.

    This is untested, but it should work as desired:
    PHP Code:
    <?php

    $sub_fields 
    = array();

    // Capture the output from `the_sub_field()` to an array
    while ( the_repeater_field'test' ) )
    {
        
    ob_start();
        
    the_sub_field'test-sub' );
        
    $sub_fields[] = ob_get_clean();
    }

    ?>

    <?php while ( $sub_field array_shift$sub_fields ) ): ?>
        <?php echo empty( $sub_fields ) ? '<p class="last-item">' '<p>'?>
            <?php echo $sub_field?>
        </p>
    <?php endwhile; ?>
    Last edited by kbluhm; Sep 8, 2011, 12:07 PM.
    ZCE

    Comment


    • #3
      thanks a lot. this does work. it does seem a bit much but wordpress is a pita so...

      Comment


      • #4
        what if you were to have different the_sub_field( 'test-sub' ); within the same while loop? like the_sub_field( 'test-sub' ); and the_sub_field( 'test-test' ); and the_sub_field( 'test-pro' );
        Last edited by wyclef; Sep 8, 2011, 12:27 PM.

        Comment


        • #5
          Care to elaborate? I'm definitely no WP guru... not sure where you're headed with that one.
          ZCE

          Comment


          • #6
            trying to do something with more fields in the loop like follows

            PHP Code:
            <?php

                    $sub_fields 
            = array();
                    
                    
            // Capture the output from `the_sub_field()` to an array
                    
            while ( the_repeater_field'test' ) )
                    {
                        
            ob_start();
                        
            the_sub_field'test-sub' );
                        
            $sub_fields[] = ob_get_clean();
                    }
                    
                    
            ?>
                    
                    <?php while ( $sub_field array_shift$sub_fields ) ): ?>
                        <?php echo empty( $sub_fields ) ? '<p class="last-item">' '<p>'?>
                            <?php echo $sub_field?>
                        </p>
                        <span><?php the_sub_field('test-sub-new'); ?></span>
                        <a href="<?php the_sub_field('test-sub-link'); ?>"></a>

                    <?php endwhile; ?>

            Comment


            • #7
              thank you so much for sharing

              Comment


              • #8
                yea, but the additional sub fields i added don't work

                Comment

                Working...
                X
                😀
                🥰
                🤢
                😎
                😡
                👍
                👎