Detect if wordpress post has at least one image

July 25, 2009

On a WordPress, it can be really handy to be able to know if a post has at least one image or not, for example for grabbing the first image and display it. To achieve this, simply paste the code below within the loop, on your index.php, search.php or archive.php file. <?php $content = $post->post_content; $searchimages = '~<img [^>]* />~'; /*Run preg_match_all to grab all the images and save the results in $images*/ preg_match_all( $searchimages, $content, $images ); // Check to see if we have at least 1 image $iNumberOfImages... [Read the full story]

Prevent post images from being displayed on homepage

April 4, 2009

When using images in your posts, you not always want them to be shown on your blog homepage. How to prevent any images from the post to be displayed on the homepage? It is easy, Simply paste this code in your functions.php file: <?php add_filter('the_content','wpi_image_content_filter',11); function wpi_image_content_filter($content){     if (is_home() || is_front_page()){       $content = preg_replace("/<img[^>]+\>/i", "", $content);     }     return $content; } ?>  Read More →

Get the first image from the wordpress post and display it

February 25, 2009

Most WordPress users are using custom fields to display thumbs on their blog homepage. It is a good idea, but do you know that with a simple php function, you can grab the first image from the post, and display it. You can simply call the function within the loop to display the first image from the post: <?php echo get_first_image() ?> But first, paste this function follow on your functions.php file. function get_first_image() { global $post, $posts; $first_img = ''; ob_start(); ob_end_clean(); $output =... [Read the full story]

Separate each wordpress post with an image

February 8, 2009

First, you need an image. Once you have it, upload it to your wp-content/themes/yourthemes/images directory, open up your index.php file, find the loop and make sure that each post is embedded throught a <div class=”post”> tag. This is the case on most themes. Then, edit your style.css file. Find the following line: .post { And paste this code: background: transparent url(images/yourimage.jpg) no-repeat bottom;  Read More →

Display the last image attached to post using shortcode

January 20, 2009

First, you have to paste the following code in your functions.php file, in order to create the shortcode. function sc_postimage($atts, $content = null) { extract(shortcode_atts(array( "size" => 'thumbnail', "float" => 'none' ), $atts)); $images =& get_children( 'post_type=attachment&post_mime_type=image&post_parent=' . get_the_id() ); foreach( $images as $imageID => $imagePost ) $fullimage = wp_get_attachment_image($imageID, $size, false); $imagedata = wp_get_attachment_image_src($imageID,... [Read the full story]