Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

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?

share|improve this question

2 Answers 2

/**
 * @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 */ }

share|improve this answer
    
Thanks alot guys for the very fast replied, its been 2 days googling but I couldn't find what I am looking for, but now I found that answer. Two thumbs up for both of you and also I've bookmarked your site to check whats new on your site. Thanks again –  kurima May 27 '11 at 6:56
    
Glad it worked. You should in any case mark one as the solution :) –  kaiser May 27 '11 at 12:41

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.