How do I count the number of media attachments a specific post has?
Output example: This post has 22 photos.
Thanks!
How do I count the number of media attachments a specific post has?
Output example: This post has 22 photos.
Thanks!
You must be logged in to post a comment.
Use this code if you’re in the loop:
If you’re not in the loop, substitute
$post->ID
with the ID of the specific post. But that should count all attachments.My take would be this:
There is no need to query for all post fields if only intent is to count amount.
Also note that attachment is not necessarily image and that is not easy (possible?) to query for, see
wp_attachment_is_image()
.