When writing a Post in WordPress you can upload images to simultaneously writing the article. These images, which are part of the media library, are associated with that particular post. The peculiarity of this behavior lies in the possibility of extracting these images no matter if they were physically placed inside the post. This process, therefore, is far more interesting than using personallizzati fields (custom fields) or "difficult" (for some) regexp (regular expressions) within the body of the post. Recently, moreover, WordPress allows you to insert a simple, yet immediate, gallery, taking advantage of this connection between your post and image loaded. Low-level images uploaded are treated exactly like a post, that is, the database table and fields are exactly the same.
For example, if a particular post - loaded with pictures - we use the following code:
1 2 3 4 5 6 | / / Where $ post_id is the id of the post / / If we are in a loop we can use $ post-ID get_children ( 'post_type=attachment&post_mime_type=image&post_parent=' . $post_id ) ; $ To = & get_children ('post_type = attachment & post_mime_type = image & post_parent ='. $ Post_id); $ai as $image ) { foreach ($ to as $ image) { . $image -> post_title . '</p>' ; echo '<p>'. $ image -> post_title. '</ p>'; } |
We get the list of titles of uploaded images. To view the images just modify the output:
1 | . $image -> post_title . '<img src="' . $image -> guid . '" /></p>' ; echo '<p>'. $ image -> post_title. '<img src="'. $image -> guid.' "/> </ p> '; |
However, it is more correct - for reasons of compatibility with future updates - use the features that WordPress offers to fetch the url of the image. For example we can see in the picture Size is real and the thumbnail image by:
1 2 3 | . $image -> post_title . echo '<p>'. $ image -> post_title. wp_get_attachment_thumb_url ( $image -> ID ) . '" />' . '<img Src="'. Wp_get_attachment_thumb_url ($image -> ID).' "/> '. wp_get_attachment_url ( $image -> ID ) . '" /></p>' ; '<img Src="'. Wp_get_attachment_url ($image -> ID).' "/> </ P> '; |
Order
In the examples we've seen over the images are recovered without any specific order for a maximum reverse order of loading. If you have ever tried to use the gallery of WordPress, you'll notice that this allows you to sort the images loaded, sorting can be done by title, date and time or a random mode. In addition you can use the manual mode, so we choose an order of images, always with respect to the concept of tunnel, then the sequence (the first, second, third, and so on ...). It is also possible to indicate how increasing or decreasing the criterion that we chose. In particular, manual sorting, works as in the pages, ie exploiting the field menu_order (initially set to 0 for all the images loaded).
So if you have uploaded a series of images and manipulated them in a post with manual sorting, we must make sure that in our cycle is taken into consideration the order. per qualsiasi tipo di campo ( menu_order , ID , data , etc…): A quick, and extremely simple to do this is to use the function usort() to tidy up our array for any type of field ( menu_order , ID , data , etc ...):
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | / / Where $ post_id is the id of the post / / If we are in a loop we can use $ post-ID get_children ( 'post_type=attachment&post_mime_type=image&post_parent=' . $post_id ) ; $ To = & get_children ('post_type = attachment & post_mime_type = image & post_parent ='. $ Post_id); / / Apply my own sort function so / / You can help in selecting the field on which to / / The sorting (in our case menu_order) $ai , "sortImage" ) ; usort ($ to, "sortImage"); $ai as $image ) { foreach ($ to as $ image) { . $image -> post_title . '<img src="' . wp_get_attachment_thumb_url ( $image -> ID ) . '" /></p>' ; echo '<p>'. $ image -> post_title. '<img src="'. wp_get_attachment_thumb_url ($image -> ID).' "/> </ p> '; } $a , $b ) { sortImage function ($ a, $ b) { $a -> menu_order == $b -> menu_order ) return 0 ; if ($ a -> menu_order == $ b -> menu_order) return 0; $a -> menu_order > $b -> menu_order ) ? 1 : - 1 ; return ($ a -> menu_order> $ b -> menu_order)? 1: - 1; / / Return ($ a-> menu_order> $ b-> menu_order)? -1: 1, / / descending } |
Needless to say, of course, if you want to recover the "before" and simply delete the single image loaded foreach() .










[...] Recover images loaded in a Post recuparare A script for images of a post, although these were not actually inserted in the text! [...]
Hello and thanks for your article. But 'I did not understand what' s the code to enter to retrieve only want to preview the image inserted in the first post ...
@ James: comment out the line as the last ten twelve, thereby eliminating the
foreach. Instead of row 10, so before youecho ...enter:I hope you have understood your question ...
@ Giovambattista Fazioli:
Giovambattista Hello and thanks for your answer. I inserted the code as you have changed and there seems to be a problem depending on usort and also the image does not appear ...
Warning: usort() [function.usort]: Invalid comparison function. in /membri2/xxxxx/wp-content/themes/nomtema/index.php on line 27Why? use WP 2.9.1
@ James: It would seem that the sort function
sortImage()is not written properly. If postami the entire code you wrote, even in email.@ Giovambattista Fazioli:
I call the php code that I inserted via email
Thank you very much
Hello,
I would like to ask you information about the script, with the introduction of wp 3.0, they added the ability to insert the thumbs, I would need to change the script so as not to appear in the list of images, one that is used thumbs as the post.
Do you know if you give me a hand?
thank you in advance,
George
@ George: you may do it this way. Before removing the images related to a post, extract any thumbnail (as indicated in WordPress 2.9 +: new feature post thumbnails ). Inside the loop extraction, then, if the image is associated with that thumbnail to exclude. If I understand correctly ...
Perfect, thank you! Among the other had thought to a solution of this type or alternatively thought to insert the thumb as the first image in the tunnel and then to exclude it through an artifice:
could be an equally effective solution for you?
Thanks for the reply,
George
@ George: that's fine also positioning it as before, just remember
Hello,
sorry if I continue to pester, but I get errors and can not understand why ...
my function is as follows:
2
3
4
$ai as $image ) { foreach ($ to as $ image) {
. $image -> post_title . '</p>' ; echo '<p>'. $ image -> post_title. '</ p>';
}
How do I make him take a check to see if there is a post thumbail and then exclude it? In my template I have enabled support for this function and everything works perfectly, just not how I understood him precisely control which element is the thumb of the post and therefore exclude it from the output ...
If you can help me out I eviteresti to slit my wrists ... I'm losing patience behind this thing and I can not deal with them ...
thanks a lot,
George