$caption_image_count = 0; function count_images(){ global $post; $thePostID = $post->ID; $parameters = array( 'post_type' => 'attachment', 'post_parent' => $thePostID, 'post_mime_type' => 'image'); $attachments = get_children($parameters); $content = count($attachments); return $content; } #add_filter('the_content','count_images'); function caption_image_callback($matches) { global $caption_image_count; $caption_image_count += 1; $c = count_images(); for ($i=1; $i <= $c; $i++) { if (is_single()) { return '
' . $caption_image_count . '
'; } else { return ''; } } } function caption_image($post_body_content) { global $caption_image_count; $post_body_content = preg_replace_callback("||","caption_image_callback",$post_body_content); $caption_image_count = 0; return $post_body_content; } if ( current_user_can('edit_plugins') ) { add_filter('the_content', 'caption_image'); }