I’ve a custom field in images named as section and each of the images have numbers, I assigned many images with number 2 and tried to execute the code above but failed.
The loop works fine without the logical IF statement
You’re calling the kirbytext() method on your field, causing it to be interpreted as markdown text— it ends up comparing the strings <p>2</p> and 2, and failing.
Try replacing that with toInt()! This will coerce the field into an integer. Then, take off the quotes from your "2" to do an integer comparison.