Hi,
I had a problem finding out how to display the “thumb” or “medium” size (as defined in WP options (http://www.url.com/wp-admin/options-media.php)) of images that were uploaded through a TDO form.
These images are saved as attachments in the WP-posts table in your MYSQL database. But how to retrieve them? And how do you know how many there are?
Here\’s the solution:
First we need to get the children of the current post, and not just all the children but only the children that have post_type: “attachment” and post_mime_type: “image”.
We use the “get_posts()” function for this.
Next we take the post_id of each attachment and use that to retrieve the image (with “wp_get_attachment_image()”) or just the URI (with “wp_get_attachment_url()”) and outpot that in our template file.
The code looks like this (I hope it outputs nice in this forum):
$args = array( \’post_type\’ => \’attachment\’,
\’post_mime_type\’ => \’image\’,
\’post_parent\’ => $post->ID
);
$images = get_posts( $args );
foreach($images as $image):
echo wp_get_attachment_image($image->ID, \’medium\’);
endforeach;
Simple as that!
Any comments? I would be happy to hear what you think about it.
if you replace the following line:
echo wp_get_attachment_image($image->ID, ’medium’);
with:
echo wp_get_attachment_image_src($image->ID, ’medium’, false);
then you get an array with the url and dimensions of the file.
use the url to create a command for phpthumb.
more info on the wp_get_attachment_image_src() function is here:
http://codex.wordpress.org/Function_Reference/wp_get_attachment_image_src
Getting these errors:
Warning: Unexpected character in input: ‘\’ (ASCII=92) state=1 in C:\xampp\htdocs\wp1\wp-content\themes\default\index.php on line 14
Warning: Unexpected character in input: ‘\’ (ASCII=92) state=1 in C:\xampp\htdocs\wp1\wp-content\themes\default\index.php on line 14
Parse error: parse error, expecting `’)” in C:\xampp\htdocs\wp1\wp-content\themes\default\index.php on line 14
Any clue?
Those backslashes didn’t belong in my code. The forum software put them there.
This is the proper code
$args = array(
'post_type' => 'attachment',
'post_mime_type' => 'image',
'post_parent' => $post->ID
);
$images = get_posts( $args );
foreach($images as $image):
echo wp_get_attachment_image($image->ID, 'medium');
endforeach;
Jane said:
I would like to insert a style class here. What is the best way to do that?
Hi Jane, let me prefix by saying there maybe an easier better way to do this. However, on my posts here I noted that using the append widget allows you to literally insert code in the space where the append widget is located. so if you made an append widget with this code in it
div style=”whateverstyle
and dropped it say above the uploads widget, then make another append widget with this code
/div
to close the style div and drop it AFTER the uploads widget. Now that style would wrap whatever content was uploaded.
So first you need to create enough append widgets to cover your opening and closing tags, probably 2 and then add them as above. Your style can be added to your main stylesheet in the ysual way. This method does work, but there maybe neater solutions. I would like to see some style classes added to this plugin that would make it all a bit cleaner.
This is a very usefull solution…
But how to retrieve the URL only for the medium sized image?
the output of should be:
http://myweb.com/wp-content/uploads/medium-image.jpg
Not:
<img src=”http://myweb.com/wp-content/uploads/medium-image.jpg” />
Thanks in advance !
to dave:
if you wish to retrieve only one image (or a fixed amount of images) then alter the foreach loop with a counter and quit the loop as soon as the counter reaches your set maximum.
or, even easier, if you only want ONE image replace:
foreach($images as $image):
echo wp_get_attachment_image($image->ID, 'medium');
endforeach;
with:
echo wp_get_attachment_image($images[0]->ID, 'medium');
to pupungbp:
my example code actualy retrieves the medium sized images.
$args = array(
'post_type' => 'attachment',
'post_mime_type' => 'image',
'post_parent' => $post->ID
);
$images = get_posts( $args );
foreach($images as $image):
echo wp_get_attachment_image($image->ID, 'medium');
endforeach;
See the second-last line in the code. You can insert here either “medium”, “thumbnail” or “blabla” (or nothing
) for the fullsize image.
Many Thanks Bram !!!
What I mean is to get rid of tag from the output of the code, all I need only the full URL to the image. So the result should be:
http://mywebsite.com/wp-content/link-to-image.jpg
Currently i’m developing my new theme:
http://demo.wordspop.com/fotofolio
I’ll give you credit for this. Again, Thanks…
Hmm, the weird thing is that my mini-form even doesn’t generate the other sizes (I have modified them manually with a plugin so they’re not the standard medium and small anymore – but this is not the case). And it also doesn’t upload them to the folders the WP media-uploader would… my uploads are categorized as uploads/2009/05, uploads/2009/04 and so on… but TDOMF uploads them to random new folders like /uploads/1290/ and such. But my whole system is built upon the year/month system so this is a little problem. How could I fix that little problem, anyone? ^^ A big thanks in advance.
Ragnar said:
Hmm, the weird thing is that my mini-form even doesn’t generate the other sizes (I have modified them manually with a plugin so they’re not the standard medium and small anymore – but this is not the case). And it also doesn’t upload them to the folders the WP media-uploader would… my uploads are categorized as uploads/2009/05, uploads/2009/04 and so on… but TDOMF uploads them to random new folders like /uploads/1290/ and such. But my whole system is built upon the year/month system so this is a little problem. How could I fix that little problem, anyone? ^^ A big thanks in advance.
I have this solution:
Uncheck Organize my uploads into month- and year-based folders in miscellaneous setting on Wordpress.
Edit tdomf-upload-functions.php in plugins/tdo-mini-forms/include
Go to line 551, replace line
$postdir = $options['path'].DIRECTORY_SEPARATOR.$post_ID;
with
$postdir = $options['path'];
Now, Wordpress and TDOMF upload the image in the same folder, wp-contents/upload.
finally, use this code to retrieve the image
$args = array(
'post_type' => 'attachment',
'post_mime_type' => 'image',
'post_parent' => $post->ID
);
$images = get_posts( $args );
foreach($images as $image):
echo wp_get_attachment_image($image->ID, 'medium');
endforeach;
This made my job much easier. Thank you.
bram said:
Hi,I had a problem finding out how to display the “thumb” or “medium” size (as defined in WP options (http://www.url.com/wp-admin/options-media.php)) of images that were uploaded through a TDO form.
These images are saved as attachments in the WP-posts table in your MYSQL database. But how to retrieve them? And how do you know how many there are?
Here’s the solution:
First we need to get the children of the current post, and not just all the children but only the children that have post_type: “attachment” and post_mime_type: “image”.
We use the “get_posts()” function for this.Next we take the post_id of each attachment and use that to retrieve the image (with “wp_get_attachment_image()”) or just the URI (with “wp_get_attachment_url()”) and outpot that in our template file.
The code looks like this (I hope it outputs nice in this forum):
$args = array( ‘post_type’ => ‘attachment’,
‘post_mime_type’ => ‘image’,
‘post_parent’ => $post->ID
);
$images = get_posts( $args );foreach($images as $image):
echo wp_get_attachment_image($image->ID, ‘medium’);
endforeach;Simple as that!
Any comments? I would be happy to hear what you think about it.
Very useful code. thanks!
If you want to add in the alt and title tags you can change the foreach code to
foreach($images as $image):
$i = wp_get_attachment_image($image->ID, 'large');
$html = str_replace("alt=\\"\\"","alt='alt text' title='title text'",$i);
echo $html;
endforeach;
Here’s where I’m currently stuck. Where do I put this code so my blog feed will display the attached images inserted through the TDO Form?
hello,
nice thread, just one question: is it possible to use this code to show up thumbnails in the preview?
I do not know how to make image thumbs visible in preview (before submitting posts); the preview just spots the text area; I’ve tried hacking this by adding the code to the preview hack form, but the preview now gets “all” the attachments for unapproved submissions.
any way to do that?
thanks on advance, ciao!
neuville said:
nice thread, just one question: is it possible to use this code to show up thumbnails in the preview?
Yes, but you have to use the right combination of options. You need to have these options checked:
“Insert Uploaded Files as Attachments on post”
“Add download link as image tag to post content”
Kate said:
Hi, sorry can you please tell me exactly where to add the code? I am a wordpress newbie and terms like ‘where there is a loop’ mean nothing to me.
John said:
If you want to add in the alt and title tags you can change the foreach code to
foreach($images as $image):
$i = wp_get_attachment_image($image->ID, 'large');
$html = str_replace("alt=\"\"","alt='alt text' title='title text'",$i);
echo $html;
endforeach;
Hey Guys,
using this method to insert thumbnails, does anyone know what i need to do to link each thumbnail to it’s original image? I want to use a lightbox effect on the thumbs…so they need to be linked to the original attachment.
thanks in advance!
Mark Cunningham said:
neuville said:
nice thread, just one question: is it possible to use this code to show up thumbnails in the preview?Yes, but you have to use the right combination of options. You need to have these options checked:
“Insert Uploaded Files as Attachments on post”
“Add download link as image tag to post content”
Mark, I’m trying to also display the thumbnail in the preview, using the advice and code above, but it isn’t working. Since the thumbnail is a result of an actual post created (I think), does the thumbnail even exist at the preview stage? I don’t see it in the ‘tmp’ folder, only the original image exists after pressing the Upload Now button and the Preview button. My site never uses the original image, only thumbnails that link to another site, so I would like to display the thumbnail in the preview because this is what will be used in the post.
I can display the large image sized to 250px using %%WIDGET:upload-files%% in the Form Hacker, but the image quality is poor. I can also use the thumbnail in the post after the post is created by using the ‘_tdomf_download_thumburi_0′ custom field, but that’s after the post is created.
Any ideas since this post on how to use a thumbnail in the preview? Thanks!
Any ideas since this post on how to use a thumbnail in the preview?
None I’m afraid. You are right, thumbnails are not generated till after the post is created. This is because TDOMF lets Wordpress handle the thumbnail generation, but it can’t do that until it’s create a post. At best you can use the uploaded image and simply resize it to the preview/thumbnail size?
I created in functions.php of my theme a custom version of the image_resize() function in media.php and called that in the /// Move the uploaded file to the temp storage path section of ‘tdomf_upload_inline.php’:
if(move_uploaded_file($upload_temp_file_name,$uploaded_file)) {
$uploaded_file = realpath($uploaded_file);
// make final 250×250 thumb to overwrite uploaded file
make_thumb($uploaded_file, 250, 250);
The only thing I customized in the make_thumb() function was to remove the default suffix from the result filename that Wordpress appends for the image width and height and set to always crop so I get a square image. After the resize, the file is the same name as the original uploaded file, so it overwrites the original with the new size in the tmp folder. If the post is approved, the image is moved to the final storage folder and Wordpress does not need to create a thumbnail. Works great.
Hello there,
I’ve been working through this post to try and make thumbnails appear once a photo has been uploaded to my site but can’t figure out where I should post this code:
$args = array(
‘post_type’ => ‘attachment’,
‘post_mime_type’ => ‘image’,
‘post_parent’ => $post->ID
);
$images = get_posts( $args );
foreach($images as $image):
echo wp_get_attachment_image($image->ID, ‘medium’);
endforeach;
Can anyone tell me which file and where I should insert this code.
Many thanks,
Julian
doobus
Guest
February 23rd, 2009 at 10:27 pm
Do you know if you can use this with conjunction with phpThumb?
Permalink | Quote