questions

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

Rate this post

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 :
?>

 

✔️Solution:

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 :
?>

Leave a Reply

Your email address will not be published.

Back to top button