The MU forums have moved to WordPress.org

Using thumbnails in posts for magazine style home page (16 posts)

  1. dwenaus
    Member
    Posted 15 years ago #

    I'm looking for a way to take the first image in a post and make a small thumbnail for use on the homepage of the blogs. I'm guessing this should be very simple, but I'm having a heck of a time attempting to do it.

    why doesn't wordpress have any image handling tools in its template tags?

  2. andrea_r
    Moderator
    Posted 15 years ago #

    Have you looked at magazine themes to see how they do it?

    Also, try this:
    http://justintadlock.com/archives/2008/05/27/get-the-image-wordpress-plugin

  3. Klark0
    Member
    Posted 15 years ago #

    Yup best way is the rip the guts out of a magazine theme and use that. Some of them of don't plugins. There's a free japanese(?) one that begins with H. can't remember the name.

  4. dwenaus
    Member
    Posted 15 years ago #

    I'll try that. I did buy an excellent magazine theme, and it came with a plugin called post thumbs revisited, looks like an excellent plugin, but it is no longer supported, and I don't believe it works with MU. I even bought a cool plugin that creates smart thumbnails (viva thumbs) but It does not work with MU I'm guessing because the way that images are stored in MU is radically different. I'll take a look at that link you sent. but if anyone has any links or advice on how to access images within the loop in MU i would be grateful.

  5. andrea_r
    Moderator
    Posted 15 years ago #

    Images are pulled from the posts table, but they have a post parent hat matches the GUID of the parent post.

    That help? :D

  6. dwenaus
    Member
    Posted 15 years ago #

    maybe I'm being too picky, but I notice that most of the thumbnail scripts work off the meta data of the post, not the actual image that is inside the post. lets say i choose an image from my gallery instead of uploading it, then I won't see a thumb. or lets say i upload an image but then do not insert it in the post. then the thumb gets shown but it is not actually in the post content.

    I should probably just ignore this small error, because 99% of the time people will simply upload, insert, and publish. hmmm.

    does anyone have any thoughts on this?

  7. andrea_r
    Moderator
    Posted 15 years ago #

    In the magazine themes, they're using the meta data to keep track of the pictures attached to the post anyway, not doing the upload-insert method.

    Yeah, I think it's kinda convoluted myself and I haven't had a chance to mess with it.

    I'm thinking this plugin:
    http://justintadlock.com/archives/2008/05/27/get-the-image-wordpress-plugin
    might be a good way around it.

  8. dwenaus
    Member
    Posted 15 years ago #

    I was using that plug in and it works, but only if the guid is set properly. For some reason It was not working out. as I have been modifying site wide tags to copy over the meta data as well. so I ended up just going with very simple code that worked great.

    'function the_thumb() {
    if ( $images = get_children(array(
    'post_parent' => get_the_ID(),
    'post_type' => 'attachment',
    'numberposts' => 1,
    'post_mime_type' => 'image',)))
    {
    foreach( $images as $image ) {
    $attachmenturl=wp_get_attachment_url($image->ID);
    $attachmentimage=wp_get_attachment_image( $image->ID, 'thumbnail' );

    echo $attachmentimage; //add a link to the image here as well using $attachmenturl
    }
    }

    }
    '
    I wonder if other people are interested in the changes needed to copy over meta data for site wide posts? if so, I'll post the changes, and maybe donncha can integrate it with the option to turn it on or off.

  9. andrea_r
    Moderator
    Posted 15 years ago #

    Yeah, you better post the code. :)

  10. dwenaus
    Member
    Posted 15 years ago #

    ok, here is how to get meta information to be passed using donncha's excellent site wide tags plugin:

    before switch_to_blog( $tags_blog_id ); on line 159 you should add the following:

    if ( $images = get_children(array( 'post_parent' => $post_id, 'post_type' => 'attachment', 'numberposts' => 1, 'post_mime_type' => 'image',)))
    	{
    		foreach( $images as $image ) {
    			$the_attachement_file = get_attached_file($image->ID);
    			$the_attachement_url = wp_get_attachment_url($image->ID);
    		}
    	}

    then after $p = wp_insert_post( $post ); about 20 lines down add the following:

    $attachment = array( 'post_title' => 'file'. time(), 'post_content' => '', 'post_type' => 'attachment', 'guid' => $the_attachement_url, 'post_parent' => $p, 'post_mime_type' => 'image/jpeg' );
    		$attach_id = wp_insert_attachment( $attachment, $the_attachement_file, '');
    		$attach_data = wp_generate_attachment_metadata( $attach_id, $the_attachement_file );
      		wp_update_attachment_metadata( $attach_id, $attach_data );

    for me it is not really passing the guid properly for some reason, but it is still working with the simple thumbnail generator i use above.

    i hope this helps. and I hope I have not forgotten anything or have coded it poorly.

  11. rizapn
    Member
    Posted 15 years ago #

    Thank you for the idea. I also learn about Donncha's sitewide tag, and use it in our photoblog gallery. I'll try to insert something like your code into that plugin to 'copy' also the_children part. Currently, I use switch_to_blog and then restor_current_blog if I want to get the thumbnail from the original post (if called from the tags site).

  12. boonika
    Member
    Posted 15 years ago #

    @dwenaus - Right now I'm using justin tadlock's plugin + coffe2code's plugin (btw, great plugin) to get other custom fields. I'm thinkig of using your solution cause I want to collect site wide posts by using donncha's SWT and I also want to show all the custom fields, including thumbs. But how can I display default thumb if there isn't any image attached to the post? Or did I miss a step or two?

  13. dwenaus
    Member
    Posted 15 years ago #

    images only get uploaded into a folder and wordpress 'attaches' the image to the post by using some combination of an entry for the attachment in the posts table as well as two entries in the postmeta table, (one has the actual path to the image file and the other has image meta data.) my modification simply copies the file path from the original post, and attaches it to the new post in the main blog. then it adds the post meta info too. It all works, but on my site I'm not sure the guid gets passed properly. maybe it does in a fresh install. but for me it does not matter because I grab the image using get_children().

    Somehow the attachment and the post are all linked up via post id. i don't fully grok it all, but I'm sure it is written somewhere. does anyone know how this linkage occurs?

  14. andrea_r
    Moderator
    Posted 15 years ago #

    There's a field called post_parent. On a regualr post the value is 0. If the "entry" is an attachment, then that field has the post ID of the parent post - the post it is attached to.

  15. whatch
    Member
    Posted 15 years ago #

    I must be missing something. I tried using the code from dwenaus in my functions.php file; however, no image thumbnails were displaying. I then tried another approach:

    $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");

    echo the_attachment_link($attachment_id);

    ...which gives "Missing Attachment". I don't understand. There are image(s) clearly within the posts and they display normally when I view the post by itself. How is Wordpress associating these images with my post? What am I missing?

  16. whatch
    Member
    Posted 15 years ago #

    Well, I found out why. I may be getting off topic here, but it seems that my images are being stored with post_parent set to 0. Any idea why? They're not stored in the gallery, but are added directly to the post itself. I'm at a loss here.

About this Topic