Skip to content
Advertisement

How to return only the last counter value in PHP for a custom field

How do you fetch the last value only from the following code. At the moment, this code prints all the rows but I just need to echo the last number only. I am using a custom repeater field that allows multiple uploads so I need the $counter variable to return the last number which I can then concatenate with Documents to get say, 5 Documents.

<?php while( have_rows('tender_docs') ): the_row(); ?>

<?php $counter++; 
echo $counter . ' Documents';
?>
<?php endwhile; 
else :
?>

Advertisement

Answer

Rather than printing the document number inside the while loop, you can simply print it after the while loop where you have the final count.

<?php while( have_rows('tender_docs') ): the_row(); ?>
<?php $counter++; 
?>
<?php endwhile;
echo $counter . ' Documents';
else :
?>
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement