• Hello guys,

    I’ve been surfing the net searching for plugins which will print a thumbnail from a post (its first image), beyond the fact the post’s text is going to be displayed or not.

    I’ve found several very neat plugins to do that, but I’m going minimalist this time so I wonder what about this function ?

    <?php echo wp_get_attachment_image( 1 ); ?>

    1- Would that do the work inside the Loop? Why those (cool) plugins exists so ?
    2- How do I call for the post_ID that this function seems to need ?
    3- Can I use it in multiple loops ?
    4- Can soemone post two different loops working side by side where this function would work ?

    Thanks very much for any answer,

    D.

Viewing 3 replies - 1 through 3 (of 3 total)
  • Thread Starter dunkkan

    (@dunkkan)

    In this topic I think I found the solution.

    Thread Starter dunkkan

    (@dunkkan)

    Okay, there we go :

    <!-- starting the loop.
         we target one category
         and get just 3 posts of it -->
    <?php
     $lastposts = get_posts('category=38&numberposts=3');
     foreach($lastposts as $post) :
        setup_postdata($post);
    ?>
    <!-- a title for each of those 3 posts,
         permalinked -->
    <h2><a href="<?php the_permalink(); ?>" id="post-<?php the_ID(); ?>"><?php the_title(); ?></a></h2>
    <!-- detecting the attachment
         in the database,
         and sorting it -->
    <?php
    global $wpdb;
    $attachment_id = $wpdb->get_var("SELECT ID FROM $wpdb->posts WHERE post_parent = '$post->ID' AND post_status = 'inherit' AND post_type='attachment' ORDER BY post_date DESC LIMIT 1");
    ?>
    <!-- printing our cool
         clickable thumb -->
    <a href="<?php the_permalink(); ?>"><?php echo get_attachment_icon($attachment_id); ?></a>
    <!-- closing the loop -->
    <?php endforeach; ?>

    If we want to repeat the same for another category in the same page, don’t forget to replace your <?php get_header(); ?> by a new <?php get_header(); rewind_posts(); ?> in the beggining of your template.

    Thread Starter dunkkan

    (@dunkkan)

    I’ve noticed an article belonging to several categories will show the ‘default’ no-image icon (?).

    I guess it must be the get_posts function, but I don’t know which array pass there. I cannot call for every possible category to prevent each combination : it would list a bunch of things.

    I need to sort just one category, which eventually will be combined with another.

    Please, anybody can help ?

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Retrieve a thumbnail from a post (wp_get_attachment_image)’ is closed to new replies.