Skip to content
Custom fields for WordPress that are quick to build and quick to load. Get Pro

Structured data and templates

Counting rows, and the first and last

Count rows and treat the first and last differently, with plain PHP.

1 minute Works with the free plugin

PHP
<?php
$slides = ugly_rows( 'slides' );
$total  = count( $slides );
?>

<?php foreach ( $slides as $i => $slide ) : ?>
	<div class="slide<?php echo 0 === $i ? ' is-first' : ''; ?><?php echo $total - 1 === $i ? ' is-last' : ''; ?>">
		<?php $slide->show( 'title' ); ?>
	</div>
<?php endforeach; ?>

ugly_rows() gives a normal PHP list, so count(), keys and array_slice() work as you expect.