WordPress Shortcode to get Total Images in a Post

Here’s a shortcode to get the total number of images in a post in WordPress.

Shortcode: [post_image_count]

Code for the shortcode:

//Total Images in a post
function sw_post_image_count(){
$wall_count = count( get_attached_media( 'image', $post->ID ) );
return $wall_count - 1;
	}
add_shortcode('post_image_count','sw_post_image_count');
PHP

Wherever you want to display the total images at post level, you can simply use the [post_image_count] shortcode.

Related:

How to add this code to your WordPress site

Leave a Comment