How to limit 1 image per post on homepage only?

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).

Read More

Anyone know how to do it??

Or any Image excerpt plugin that can limit image on homepage?

Related posts

Leave a Reply

2 comments

  1. /**
     * @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='{$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 */ }

  2. I use this inside your themes functions.php

    // function (image toolbox) for all images
     function attachment_toolbox($size = thumbnail) { 
    
    if($images = get_children(array(
        'post_parent'    => get_the_ID(),
        'post_type'      => 'attachment',
        'numberposts'    => 1, // show all images is -1
        'post_status'    => null,
        'post_mime_type' => 'image',
    ))) {
        foreach($images as $image) {
            $attimg   = wp_get_attachment_image($image->ID,$size);
    
            $postlink = get_permalink($image->post_parent);
    
    
            echo '<a href="'.$postlink.'">'.$attimg.'</a>';
    
        }
    }
    

    }

    and call it inside your loop with

    <div class="around_image"> 
      <?php attachment_toolbox('medium'); ?>
    </div><!-- / around_image -->
    

    thats straight out of my site im working on right now, change “medium” to the size of the pic you want, “thumbnail”, “medium”, “large” or “full_size”

    for more info go here