[1]

bram
Guest
February 19th, 2009 at 5:04 pm

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.

Permalink | Quote

[2]

doobus
Guest
February 23rd, 2009 at 10:27 pm

Do you know if you can use this with conjunction with phpThumb?

Permalink | Quote

[3]

bram
Guest
February 24th, 2009 at 8:43 am

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

Permalink | Quote

[4]

Mark Cunningham
Guest
February 24th, 2009 at 10:42 am

Bloodly good work Bram. Thank you. I’ll be directly people to this thread from now on on questions about image size. :)

Permalink | Quote

[5]

bram
Guest
February 24th, 2009 at 1:02 pm

happy to hear I could help out a bit.

TDO forms saved my day ;)

Permalink | Quote

[6]

doobus
Guest
February 25th, 2009 at 10:38 pm

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?

Permalink | Quote

[7]

bram
Guest
February 26th, 2009 at 8:51 am

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;

Permalink | Quote

[8]

Richard
Guest
March 6th, 2009 at 4:59 pm

Bram, can you tell me where this code goes?

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

Permalink | Quote

[9]

Mark Cunningham
Guest
March 9th, 2009 at 12:25 pm

You could put the code in your theme for example. Anywhere there is the loop, such as index.php or single.php.

Permalink | Quote

[10]

Richard
Guest
March 9th, 2009 at 2:23 pm

Thanks Mark for your reply!

Permalink | Quote

[11]

dave
Guest
March 14th, 2009 at 2:22 pm

bram / mark,

thanks for this. I’ve inserted the code into index.php but am finding it is inserting ALL the thumbnail images associated with the post.

is there a way of limiting the code so that it only inserts the first image in any given post’s thumbnail?

thx in advance.

Dave

Permalink | Quote

[12]

Jane
Guest
March 22nd, 2009 at 5:44 pm

I would like to insert a style class here. What is the best way to do that?

Permalink | Quote

[13]

Richard
Guest
March 23rd, 2009 at 9:50 am

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.

Permalink | Quote

[14]

pupungbp
Guest
March 24th, 2009 at 12:31 am

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 !

Permalink | Quote

[15]

Bram
Guest
March 26th, 2009 at 11:09 am

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.

Permalink | Quote

[16]

dave
Guest
March 26th, 2009 at 11:12 am

Bram, you’re a star. many thanks

Permalink | Quote

[17]

pupungbp
Guest
March 26th, 2009 at 4:36 pm

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…

Permalink | Quote

[18]

pupungbp
Guest
March 26th, 2009 at 4:43 pm

Sorry, the forum deleting my code, what I mean is to get rid of

<img /> from the result, Many thanks for the help…

Permalink | Quote

[19]

Bram
Guest
March 26th, 2009 at 4:46 pm

@Pupungbp

See my third post in this thread.

Permalink | Quote

[20]

Bram
Guest
March 26th, 2009 at 4:47 pm

@pupungp
That should be my SECOND post (the thid post in this thead)

Permalink | Quote

[21]

pupungbp
Guest
March 27th, 2009 at 1:18 am

Thanks a lot Bram !!!

I’m very very thankfull for this.

Permalink | Quote

[22]

tarjo
Guest
May 22nd, 2009 at 5:25 am

Where those code should be paste?
I have add that to my Single.php, and nothing happen..

It will only show image if I upload/post from Write post at wp-admin, and not if I post from TDMOF..

please help..

thanks

Permalink | Quote

[23]

Ragnar
Guest
May 22nd, 2009 at 12:19 pm

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.

Permalink | Quote

[24]

tarjo
Guest
May 23rd, 2009 at 9:07 am

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;

Permalink | Quote

[25]

Drango
Guest
June 1st, 2009 at 7:59 pm

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.

Permalink | Quote

[26]

John
Guest
June 10th, 2009 at 9:22 pm

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?

Permalink | Quote

[27]

neuville
Guest
June 13th, 2009 at 6:01 pm

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!

Permalink | Quote

[28]

Kate
Guest
August 27th, 2009 at 12:32 am

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.

thanks in advance:)

Permalink | Quote

[29]

Mark Cunningham
Guest
August 27th, 2009 at 3:30 pm

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.

http://codex.wordpress.org/The_Loop

Permalink | Quote

[30]

dave
Guest
October 23rd, 2009 at 2:11 pm

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!

Permalink | Quote

[31]

Lee
Guest
November 3rd, 2009 at 5:17 am

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!

Permalink | Quote

[32]

Mark Cunningham
Guest
November 9th, 2009 at 9:37 am

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?

Permalink | Quote

[33]

Lee
Guest
November 9th, 2009 at 9:01 pm

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.

Permalink | Quote

[34]

Bikkuri
Guest
November 24th, 2009 at 12:41 am

After working through the extremely helpful forums, I only have one more question that I can’t seem to figure out. How can I insert the large version of the image in the DRAFT? I can only get the thumbnail to show up no matter how I hack hack hack :)

Thanks in advance.

Permalink | Quote

[35]

Tomas
Guest
December 5th, 2009 at 4:31 pm

Hello,

THank you for such a great tweak. I found it very useful. I am facing only one problem. I upload 10 files through tdo mini form and use your tweak to show these images. It shows ONLY 5 of them (instead of 10). Where could be the problem??

Thank you

Permalink | Quote

[36]

Julian
Guest
December 18th, 2009 at 2:34 pm

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

Permalink | Quote

[37]

Adam
Guest
December 27th, 2009 at 3:54 am

Easier way, add this to style.css

img {max-width:100%;
max-height:100%;

}

It will display max as thumbnail but will not brake sidebar when picture added to post through tdo mini forms.

regards,
Adam

Permalink | Quote

[38]

Tim
Guest
December 31st, 2009 at 8:32 pm

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.

Permalink | Quote

[39]

Alexander Rea
Guest
January 18th, 2010 at 2:13 am

Bravo. Thank you very much. @alexanderrea

Permalink | Quote

Advertisments

Join the Discussion