most of my post on wordpress got 6 or 5 images perpost. My problem is if I posted 5 new post with new images, it will be totall 25 images display on my homepage.
I want to limit 1 image perpost on “homepage only”, the full content (lets say got total 6 images, it will be on single page).
Anyone know how to do it??
Or any Image excerpt plugin that can limit image on homepage?
/**
* @author: wpreceipes
* @link: [1]: http://www.wprecipes.com/how-to-get-the-first-image-from-the-post-and-display-it
*/
function wpse18215_catch_that_image()
{
global $post, $posts;
$first_img = '';
ob_start();
ob_end_clean();
$output = preg_match_all( '/<img.+src=[\'"]([^\'"]+)[\'"].*>/i', $post->post_content, $matches );
$first_img = $matches[1][0];
//Defines a default image
if( empty( $first_img ) )
{
$first_img = '/images/default.jpg';
}
return $first_img;
}
Example:
echo catch_that_image();
or:
/**
* @author: Marcelo Mesquita
* @link: http://marcelomesquita.com/
* @param (string) $size - valid: 'thumbnail', 'medium', 'large' or 'full_size'
* @param (string) $add - any additional attributes for the html-img tag
*/
function wpse18215_the_thumb( $size="medium", $add = '' )
{
global $wpdb, $post;
$thumb = $wpdb->get_row(
"SELECT ID,
post_title
FROM {$wpdb->posts}
WHERE post_parent = {$post->ID}
AND post_mime_type
LIKE 'image%'
ORDER BY menu_order"
);
if( ! empty( $thumb ) )
{
$image = image_downsize( $thumb->ID, $size );
return "<img src="https://wordpress.stackexchange.com/questions/18215/{$image[0]}" alt="{$thumb->post_title}" {$add} />";
}
}
Example:
echo wpse18215_the_thumb( 'medium', 'class="alignleft" width="200" height="175"' );
Note: You need to wrap the call anyway inside your template into a conditional statement:
if ( is_home() || is_front_page() ) { /* place the function call here */ }