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
Tim said:
Why is the question being dodged as to where this code needs to be inserted. People are asking and there’s no answer. Somebody mentioned index.php or single.php but where in the hell does this code go? Please.
Actually this has been answered several times. The code must be inserted in to The Loop of your single and index page. Information about The Loop is at and it varies from theme to theme
What would be the easiest way to only show the thumbnails on these posts and not standard posts. They are all assigned to New Release category and I can set the index to a custom page for that category but not sure the best way to handle single page issues. Trying to think of the best way to test for category and only show that code for the new release category.
Hello there,
I would like to output only the first image of all attached images of a post. I know this has been answered already further up, but it did not work for me.
I followed the instruction and used this code in the foreeach loop:
echo wp_get_attachment_image($images[0]->ID, 'medium');
What happens is that it outputs the first image, but twice!
I am using WordPress 2.9.2, no other plugins installed.
An workaround or any hints would be greatly appreciated!
@bram, thank you very much for the idea
As newbie, I confuse where to place the code, so I try this way..
Add this function on functions.php
# Retrieve image for tdo miniform
function tdoimage ($postid=0, $attributes=''){
if ($postid 'attachment',
'post_mime_type' => 'image',
'numberposts' => 1,
'post_parent' => $postid
);
$images = get_posts( $args );
foreach($images as $image):
echo wp_get_attachment_image($image->ID, 'medium');
endforeach;
}
Then I call the function from single.php by placing before
I hope it will help for others.
TDO mini forms is an awesome plugin BUT I still can’t figure out how to include medium sized thumbs in posts. I nearly took down my entire site trying to implement Davina’s code above. This is because of my lack of knowledge of code. Can anyone tell me really clearly where this code goes? You’ll make my day.
Thanks.
Bram said:
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.
Cool beans. I’m using this awesome code, and it’s hooking the image in, but the custom fields aren’t showing. Isn’t there some way to just have the results of the entire form post? X)
I also just want to say that I did read through every post. I felt kind of annoyed that there were so many repeat questions. X) I figured I’d add what I thought might work as a solution:
$args = array(
‘post_type’ => ‘custom field’,
‘post_parent’ => $post->ID
);
$cf = get_posts( $args );
foreach($cf as $cf):
echo wp_get_post_custom($cf->ID);
endforeach;
I’d also like to add: I think that the creators of TDO Mini Forms could have thought this module through a lot better. This functionality should happen on the front end. The majority of people creating custom forms are going to want the submissions to be spit out on the public front end, somewhere, by default.
Julian said:
TDO mini forms is an awesome plugin BUT I still can’t figure out how to include medium sized thumbs in posts. I nearly took down my entire site trying to implement Davina’s code above. This is because of my lack of knowledge of code. Can anyone tell me really clearly where this code goes? You’ll make my day.Thanks.
I’m having the same problem. In the end, none of this code actually seems very useful. What the code does is, it plops a medium-sized image onto the post itself which doesn’t come over to the main page/article list. So, I just changed the size of thumbnails to 400×300 because I don’t use thumbnails, anyway. I imagine if I need thumbnail-size images, now, … I’ll just change the size of medium images to 150×150.
What’s happening is actually this: In the upload widget, everybody probably has “Add thumbnail as download link to post content (if thumbnail avaliable)”, which is what we are probably all looking for, only in ‘medium’ as well as ‘thumbnail’. Now, you can’t wash thumbnail for medium in the hacker; however, I imagine it can be done on the back end, somewhere in the plugin. I’ll grep thumbnail when I get shell access. Meanwhile, please post if you can figure it out faster than I can.
doobus
Guest
February 23rd, 2009 at 10:27 pm
Do you know if you can use this with conjunction with phpThumb?
Permalink | Quote