• If an image is uploaded through the media manager, and inserted into a post at the time it is created – is there some easy way to extract the ID of that new image from within the template code?

    What I really want to be able to do is get at the +thumbnail+ for a full-size image that is uploaded in a new post, so it can be included in a “table of contents” on the home page.

    Right now I use a kludgy way: examining get_the_content, doing some regexp operations, and then looking at the file-system to determine the thumbnail’s name and create an appropriate img src.

    There has to be a better way, right?

Viewing 8 replies - 1 through 8 (of 8 total)
  • Rather simple, here’s one way… the attachment is just a different data type in the posts table. (Replace $id with your post’s id)

    $files = get_children("post_parent=$id&post_type=attachment&post_mimi_type=image");

    Look in wp-includes/post.php for the (somewhat undocumented) utility functions, and use them if they match your needs. Hopefully the code will help you determine where the stuff you need is stashed.

    One example, this returns a URL (the id is the attachment‘s id):

    $link = wp_get_attachment_url($id);

    You may want wp_get_attachment_thumb_file() – Retrieve thumbnail for an attachment, or wp_get_attachment_thumb_url() – Retrieve URL for an attachment thumbnail

    Thread Starter ccwordpress

    (@ccwordpress)

    Ah-ha, I see now. I had found wp_get_attachment_thumb_url() already – but “get_children is the key I was missing. Thank you very much!

    Thread Starter ccwordpress

    (@ccwordpress)

    Just for the benefit of anyone searching on this in the future, here is what I ended up with. Called from a post loop, it will print a thumbnail if it exists. (note also a tiny typo correction from the above – there’s no “post_mimi_type” that I know of)

    <?php
    $files = get_children("post_parent=$id&post_type=attachment&post_mime_type=image");
    if($files){
            $keys = array_keys($files);
            $num=$keys[0];
            $thumb=wp_get_attachment_thumb_url($num);
            print "<img src=$thumb>";
    }
    ?>

    Hope this helps someone else out.

    @ccwordpress – you are the man!

    @ccwordpress – thanks for the typo tip – that’s what I get for cut and paste… now if I could remember where I cut/pasted from I could pass it on…

    Of course, since my intent was to enumerate a bunch of mov files I had attached (which have no thumbs and aren’t images), I guess I should change the mime type.

    Thanks guys that was really useful.

    A tip for the begginers like me:

    If you wrapp the whole code with that <a href="<?php the_permalink() ?>"> thecodepostedabove </a> you make the thumbnail click to the post.

    By the way, I’m trying to set three posts like this (excerpts):

    thumb—–thumb—–thumb
    blabla—–blabla——blabla
    blabla—–blabla——blabla
    blabla—–blabla——blabla

    But the code avobe only shows the first thumbnail for the first post.

    I use this loop for the first post:

    <?php
     $posts = get_posts('numberposts=1&category=18');
     foreach($posts as $post) :
        setup_postdata($post);
     ?>
    <!-- the code to print the thumbnail -->
    <a href="<?php the_permalink() ?>"><?php
    $files = get_children("post_parent=$id&post_type=attachment&post_mime_type=image");
    if($files){
            $keys = array_keys($files);
            $num=$keys[0];
            $thumb=wp_get_attachment_thumb_url($num);
            print "<img src=$thumb class=fade>";
    }
    ?></a>
    <?php the_excerpt(); ?>
    <?php endforeach; ?>

    In the second and third post I use an &offset=1 and &offset=2 respectively which I add to the get_posts function. I have put as well the rewind_posts function at the begining of the template, so the three excerpts shows perfectly.

    Thanks for any tip.

    Hello, I worked with some CSS for that layout, to avoid setting multiple loops.
    But even then, it seems like sometimes the code doesn’t scan for the attachment in the post …

    In some posts works like a charm, but in others the attachment keeps hidden.

    Any idea ?

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘attachments associated with a post’ is closed to new replies.