Search for images within a post
-
Hey,
I am currently looking to integrate Adsense directly into my posts but I first I need to do something.
Due to my design, I am looking for a simple way to check if there is an image in the post. I only plan to do this on single pages. My plan is if there is a image in my post, no ad is shown. If there isn’t a image, the ad is shown.
Thanks in advance.
Nevermind, sorted. I found some code online that was able to help me. It is posted below incase someone needs it.
<?php $content = $post->post_content; $searchimages = '~<img [^>]* />~'; /*Run preg_match_all to grab all the images and save the results in $pics*/ preg_match_all( $searchimages, $content, $pics ); // Check to see if we have at least 1 image $iNumberOfPics = count($pics[0]); if ( $iNumberOfPics > 0 ) { // Your post have one or more images. } ?>
- The topic ‘Search for images within a post’ is closed to new replies.