WordPress Shortcode to get Total Images in a Post

Srivishnu Ramakrishnan
Srivishnu Ramakrishnan
Engineer & Web Creator
LinkedInTwitter

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

Shortcode:[post_image_count]

Code for the shortcode

php
//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');

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

How to add this code to your WordPress site

How to Add PHP Code to WordPress