Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Limit the acf repeater field output with the aid of array_slice

So far i had an acf repeater field which was outputted as list items in unordered lists with the following type of code:

<figcaption class="bottombox fp-bb">
    <h3 class="bran-bn fp-pro-title"><?php the_title(); ?></h3>
    <?php if(have_rows( 'pro_services' ) ): ?>
    <ul class="mus-li fp-pro-serv">
    <?php while( has_sub_field( 'pro_services' ) ): ?>
        <li class="fp-lister"><?php the_sub_field( 'pro_service_single' ); ?></li>
    <?php endwhile; ?>
    </ul>
    <?php endif; ?>
    <p class="fp-pro-excerpt"><?php acf_excerpt( 'pro_description', 20, ' <span class="fp-pro-more">[...]</span>' ); ?></p>
</figcaption>

Now i have the problem that the space on my front-page is limited for the inline list items since their number has risen in a few cases. So i wanted to bypass that issue and limit the output number of list items on the front-page to two items per object max. Problem is i am unable to get things running properly. my code so far:

<figcaption class="bottombox fp-bb">
    <h3 class="bran-bn fp-pro-title"><?php the_title(); ?></h3>
    <?php if(have_rows( 'pro_services' ) ): ?>
    <ul class="mus-li fp-pro-serv">
    <?php while( has_sub_field( 'pro_services' ) ):
    $singleserv = get_sub_field('pro_services_single');
         endwhile;
    $singleservslice = array_slice($singleserv, 0, 2);
    foreach($singleservslice as $singleservcount) { ?>
        <li class="fp-lister"><?php print_r($singleservcount); ?></li>
    <?php } ?>
    </ul>
    <?php endif; ?>
    <p class="fp-pro-excerpt"><?php acf_excerpt( 'pro_description', 20, ' <span class="fp-pro-more">[...]</span>' ); ?></p>
</figcaption>

pro_services has repeater as type and the pro_services_single is the repeater field defined "within". the array_slice is working, i've tried a stand alone test case before. any advice is welcome. best regards ralf

1 Answer

Ok figured things out finally after endless tries. The solution looks that way. If anyone has recommendations how to write it in a more clean way i would be happy. best regards Ralf

<figcaption class="bottombox fp-bb">
    <h3 class="bran-bn fp-pro-title"><?php the_title(); ?></h3>
    <?php
    if(have_rows( 'pro_services' ) ): ?>
    <ul class="mus-li fp-pro-serv">
    <?php
    $fp_list_slice = array_slice( get_field('pro_services'), 0, 2 );
    foreach($fp_list_slice as $fp_list_count) { ?>
        <li class="fp-lister"><?php
        echo $fp_list_count['pro_service_single'];
        ?></li><?php } ?>
    </ul>
    <?php endif; ?>
    <p class="fp-pro-excerpt"><?php acf_excerpt( 'pro_description', 20, ' <span class="fp-pro-more">[...]</span>' ); ?></p>
</figcaption>