
Many of you asked me how to get or count the images contained within the content of a post. I’m very happy to provide you a working and easy to use solution for this highly requested recipe.

Many of you asked me how to get or count the images contained within the content of a post. I’m very happy to provide you a working and easy to use solution for this highly requested recipe.
To achieve this recipe, simply paste the following code on one of your theme files. It will search for all images contained in the post content, and display it.
<?php if (have_posts()) : ?>
<?php while (have_posts()) : the_post(); ?>
<?php
$szPostContent = $post->post_content;
$szSearchPattern = '~<img [^\>]*\ />~';
// Run preg_match_all to grab all the images and save the results in $aPics
preg_match_all( $szSearchPattern, $szPostContent, $aPics );
// Check to see if we have at least 1 image
$iNumberOfPics = count($aPics[0]);
if ( $iNumberOfPics > 0 ) {
// Now here you would do whatever you need to do with the images
// For this example the images are just displayed
for ( $i=0; $i < $iNumberOfPics ; $i++ ) {
echo $aPics[0][$i];
};
};
endwhile;
endif;
?>,
Many thanks to Matt Varone for this very nice hack!
35 Responses
I don’t get it. Why would you want to count the pictures in a post?
@Agolf Cartson: The count is only here to make sure we don’t try to display pics that doesn’t exists. If there’s one or more pics in the post content, the pics are displayed, otherwise nothing is shown.
In this code, the pictures are simply displayed, but you could easily modify it to create a gallery or something like this
I might not get my full 5 hours of sleep last night but I still don’t get why you want to count the pictures. How would anyone try to display pictures that doesn’t exist and if the pictures are shown then who needs a count? I’m sorry but I just don’t get it
Imagine your blog have 150 posts. How can you remember that post #29 that you wrote one year ago, have a picture or not? Counting make WP doing that job for you
Okay now I get what it is for. Thanks. But I still don’t get what the practical usage is. Why would anyone want to count how many pictures they have in their entire portfolio of posts? But I guess that since you have gotten requests for it some people want that. It is just beyond me
There is another way to echo an image that may be in a post.
ID;$the_content =$wpdb->get_var("SELECT post_content FROM $wpdb->posts WHERE ID = $id");
$home = get_option('home');
$pattern = '!
I wrote an article about this on my site: WPCult
So what would be the difference between the two?
sorry, the code showed up odd.
<?php
$id =$post->ID;
$the_content =$wpdb->get_var(“SELECT post_content FROM $wpdb->posts WHERE ID = $id”);
$home = get_option(‘home’);
$pattern = ‘!<img.*?src=”‘.$home.’(.*?)”!’;
preg_match_all($pattern, $the_content, $matches);
$image_src = $matches['1'][0]; ?>
there’s a easier way to do so, just by getting the first image attached to a post, like so:
function get_first_attachment() {
global $post;
if ( $output != ” )
return ”;
$id = $post->ID;
$attachments = get_children( array(‘post_parent’ => $id, ‘post_status’ => ‘inherit’, ‘post_type’ => ‘attachment’, ‘post_mime_type’ => ‘image’, ‘order’ => ‘DESC’, ‘orderby’ => ‘menu_order ASC’) );
$tpl = get_bloginfo(‘template_url’);
$nothing = $tpl.’/nothing.jpg’;
if ( empty($attachments) )
return $nothing;
$output = “\n”;
foreach ( $attachments as $id => $attachment )
$link = wp_get_attachment_url($id);
return $link;
}
you have to put a nothing.jpg in your template folder,
if you attach an image in your post, it will return the image link, else will return the nothing.jpg (you have to put a nothing.jpg in your template folder),
simply apply inside the loop,
cheers,
KL
No really, why would I want to count the images on a post. I have watched paint dry and grass grow so really why would anyone what to know this?
I’m impressed you really know how to handle PHP Codes with wordpress!
Trackbacks: