Support

Account

Home Forums Add-ons Repeater Field Limit the output of a repeater field with array_slice

Helping

Limit the output of a repeater field with array_slice

  • I have a repeater field which is output as list items inside an unordered list:

    <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 on my front-page the space for the text is limited so i wanted to limit the output to only two items max. But so far i’ve failed to accomplish it. I got this 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');
    	$singleservslice = array_slice($singleserv, 0, 2);
    	foreach($singleservslice as $singleservcount) { ?>
    		<li class="fp-lister"><?php print_r($singleservcount); ?></li>
    	<?php } ?>
    	<?php endwhile; ?>
    	</ul>
    	<?php endif; ?>
    	<p class="fp-pro-excerpt"><?php acf_excerpt( 'pro_description', 20, ' <span class="fp-pro-more">[...]</span>' ); ?></p>
    </figcaption>

    But i am probably using the wrong functions and arrays cuz the output isnt the content of the repeater field but something like: Array ([0] => Array([pro_service_single … and so on. i’ve used array_slice to cut the array down to two objects max. pro_services field type is repeater and pro_services_single is the repeater field itself. If anyone might have a hint and or idea would be cool. Thanks Ralf

  • Ok i think i’ve figured things out. If anyone knows a more clean way i would be curious how to accomplish but for the moment the following just works out fine. 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>
Viewing 2 posts - 1 through 2 (of 2 total)

The topic ‘Limit the output of a repeater field with array_slice’ is closed to new replies.