Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    Regular Coder
    Join Date
    Feb 2005
    Posts
    191
    Thanks
    6
    Thanked 0 Times in 0 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
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    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; 09-08-2011 at 05:07 PM.

  • #3
    Regular Coder
    Join Date
    Feb 2005
    Posts
    191
    Thanks
    6
    Thanked 0 Times in 0 Posts
    thanks a lot. this does work. it does seem a bit much but wordpress is a pita so...

  • #4
    Regular Coder
    Join Date
    Feb 2005
    Posts
    191
    Thanks
    6
    Thanked 0 Times in 0 Posts
    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; 09-08-2011 at 05:27 PM.

  • #5
    Senior Coder kbluhm's Avatar
    Join Date
    Apr 2007
    Location
    Philadelphia, PA, USA
    Posts
    1,509
    Thanks
    3
    Thanked 258 Times in 254 Posts
    Care to elaborate? I'm definitely no WP guru... not sure where you're headed with that one.

  • #6
    Regular Coder
    Join Date
    Feb 2005
    Posts
    191
    Thanks
    6
    Thanked 0 Times in 0 Posts
    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; ?>

  • #7
    New to the CF scene
    Join Date
    Aug 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thank you so much for sharing

  • #8
    Regular Coder
    Join Date
    Feb 2005
    Posts
    191
    Thanks
    6
    Thanked 0 Times in 0 Posts
    yea, but the additional sub fields i added don't work


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •