RSS Feeds

Posts tagged custom function

Automatically Get Images on Post Content

The problem. Using custom fields to display images associated with your post is definitely a great idea, but many WordPress users would like a solution for retrieving images embedded in the post’s content itself.

The solution. As far as we know, there’s no plug-in to do that. Happily, the following loop will do the job: it searches for images in post content and displays them on the screen.

<?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;
?>

Wordpress Adsense Shortcode

function showads() {  
 return '<div id="adsense"><script type="text/javascript"><!--  
 google_ad_client = "pub-XXXXXXXXXXXXXX";  
 google_ad_slot = "4668915978";  
 google_ad_width = 468;  
 google_ad_height = 60;  
 //-->  
 </script>  
 
 <script type="text/javascript"  src="http://pagead2.googlesyndication.com/pagead/show_ads.js">  
 </script></div>';  
 }  
 
 add_shortcode('adsense', 'showads');