
WordPress creates it flattering easy to upload design on posts as well as pages. Thereby have been assorted interpretation stored, that can be used.
Again as well as again, we can review tutorials on how to pierce photos to posts, routinely this is a fortitude around a law fields. But there is a opposite as well as simpler solution, we think. The upkeep of law fields is not regularly necessary.
Therefore, we would identical to to uncover a little solutions that are, in my opinion, most improved as well as can be stretched in most ways.
With law fields
In a initial case, a fortitude regulating a law margin to be displayed, whilst a interpretation contingency be confirmed in this margin to a key.
In chronicle 1 we store a worth of a authors to a pass author link, so it’s easy to assimilate a have make make make make make use of of of of of of of a fields. More report about this can be found in a Codex.
Version 1

The outlay of a pass is handed over by regulating a template tab get_post_meta(). The duty expects a Id of a post as well as a pass worth as a mandatory, or otherwise we can set if a worth or arrays have been allowed. Put on true as well as it earnings usually a string
/**
* @param int $post_id Post ID.
* @param fibre $key The meta pass to retrieve.
* @param bool $single Whether to lapse a singular value.
* @return churned Will be an form if $single is false. Will be worth of meta interpretation margin if $single
* is true.
*/
function get_post_meta($post_id, $key, $single = false)
<?php $key = 'authorlink'; ?>
<p>Photo of <a href="<?php echo get_post_meta($post->ID, $key, true);?>"></a>.</p>
The subsequent step is fundamentally no different, solely that we right away put a residence in a worth to an additional key, where a design is. This requires a writer to know residence as well as save a URL.

In chronicle 2, however, we go a single step serve as well as put a duty in to a functions.php, that takes caring of it as well as afterwards we can feed it with data. Again, this is usually an e.g. as well as needs in this chronicle a pass of a law fields, a breadth as well as tallness of a images. These 3 representation values, we afterwards write in a tab to outlay a image.
Version 2
/*Custom Field Images*/
function image_attachment($key, $width, $height) {
global $post;
$custom_field = get_post_meta($post->ID, $key, true);
if ($custom_field) {
echo '<img src="' . $custom_field . '" alt="Post Image" width="' . $width . '" height="' . $height . '" />';
} else {
return;
}
}
The outlay of a template is dynamic by a on top of duty as follows.
<?php
$image_key = 'image';
$myimage = get_post_meta($post->ID, $image_key, true);
if ($myimage) { ?>
<div class="post-image">
<?php image_attachment($image_key, 512, 200); ?>
</div>
<?php } ?>
This unequivocally can have a little pleasing things. Many thesis authors would identical to to usually have a design to a post, that is but delay taken at assorted points. Often this is completed by a on top of solution. However, this requires that a authors need to know a law fields as well as say them, so a pass of a law margin as well as URL to a image.
Not unequivocally comfortable, so we wish to uncover a solution, how we can get a design that was uploaded to a post, that can be found in a Gallery of a post.

WordPress own functions for images
One of a most options is wp_get_attachment_image(), that represents in my view, a simplest solution. Here is an e.g. of what explains it in some-more detail.
At initial we usually get all pictures, not all a attachments of a posts that has been uploaded but delay to a post. These images, we can but delay display. The syntax contingency regularly be in a loop.
<?php
$attachments = get_children( array('post_parent' => get_the_ID(), 'post_type' => 'attachment', 'post_mime_type' => 'image') );
foreach ( $attachments as $attachment_id => $attachment ) {
echo wp_get_attachment_image($attachment_id);
} ?>
The on top of e.g. loads around a double behind all images as well as arrangement them. We do not take caring about a tangible distance of a image, in default it will arrangement a thumbnail. By environment post_mime_type = image, it usually fetches images as well as not pick attachments of a post.
The duty of WordPress, that outputs a design with HTML, requieres a ID of a attachment, so we have to collect a ID to a post beforehand, we’ll do with get_children().
A couple of difference right away to duty wp_get_attachment_image() of WP to arrangement a images. As already mentioned, there have been multiform functions that all have a identical structure, as well as possibly give behind pick markup or concede pick parameters. For a elementary output, this duty is best. It is additionally described in a Codex.
The duty allows for 3 parameters:
/**
* Get an HTML img component representing an design attachment
*
* @param int $attachment_id Image connection ID.
* @param fibre $size Optional, default is 'thumbnail'.
* @param bool $icon Optional, default is false. Whether it is an icon.
* @return fibre HTML img component or dull fibre on failure.
*/
function wp_get_attachment_image($attachment_id, $size = 'thumbnail', $icon = false)
You get a markup behind for a img-Tag, identical to a following example.
<mg width="150" height="150" src="http://example.com/wp-content/uploads/2009/08/DSC00261-150x150.jpg"; class="attachment-thumbnail2 alt="Image Example" title="Example Image" />
The final of a 3 parameters is not unequivocally engaging in organisation with pictures, given it displays an icon. But a second parameter is there even some-more exciting, since this allows to set a distance of a image. Here we can give possibly a single of a preconfigured sizes, a sizes that we can configure in a settings of WordPress as well as have been combined when we upload regulating a media library. The following options have been available.
- Default-Values:
thumbnail, medium, large oder full
- Custom sizes around Array: array( width, tallness ) Example:
array(100, 100)
If we pass your own values, afterwards WordPress fetches a suitable design in coherence of these values. So if we set, for example, 100×100 as well as a thumbnails have been 150px150px, afterwards these images have been drawn. If we have your own form of 200×200, it will outlay a middle design since a thumbnail is as well small.
<?php
$attachments = get_children( array('post_parent' => get_the_ID(), 'post_type' => 'attachment', 'post_mime_type' =>'image') );
foreach ( $attachments as $attachment_id => $attachment ) {
echo wp_get_attachment_image( $attachment_id, array(200, 250) );
} ?>
Images with links
Now we will not regularly usually a pictures, though additionally have a design with a couple to a design in strange size. In that box there is a duty wp_get_attachment_link(). Here an example:
<?php
$attachments = get_children( array('post_parent' => get_the_ID(), 'post_type' => 'attachment', 'post_mime_type' =>'image') );
foreach ( $attachments as $attachment_id => $attachment ) {
echo wp_get_attachment_link( $attachment_id, 'medium' );
} ?>
The HTML looks identical to this.
<a href="http://example.com/wp-content/uploads/2009/08/DSC00261.JPG" title="Example Image"><img src="http://example.com/wp-content/uploads/2009/08/DSC00261-150x150.jpg" class="attachment-thumbnail" alt="Image Example" title="Example Image" height="150" width="150"/></a>
Thus a design in a requested distance is generated, as well as a couple was set to a design in strange size.
Determine distance of a images
Sometimes we wish to know in allege how large is a image. There is a duty that earnings a values as an array. This is an e.g. of a output:
<?php
$attachments = get_children( array('post_parent' => get_the_ID(), 'post_type' => 'attachment', 'post_mime_type' => 'image') );
foreach ( $attachments as $attachment_id => $attachment ) {
$src = wp_get_attachment_image_src( $attachment_id, 'full' );
var_dump($src);
} ?>
This elementary double behind to outlay a values earnings an array.
array
0 => fibre 'http://example.com/wp-content/uploads/2009/08/DSC00261.JPG' (length=63)
1 => int 1632
2 => int 1224
3 => boolean false
The sequence of a form is allocated as follows.
- $src[0] => url
- $src[1] => width
- $src[2] => height
- $src[3] => icon
Thus, it can conflict to a size, depending on requirements. Alternatively, we can work with a duty image_get_intermediate_size($post_id, $size='thumbnail'), that earnings a little some-more values. A representation of a outlay for ‘medium’:
array
'file' => fibre 'DSC00261-300x225.jpg' (length=20)
'width' => fibre '300' (length=3)
'height' => fibre '225' (length=3)
'path' => fibre '2009/08/example-300x225.jpg' (length=28)
'url' => fibre 'http://example.com/wp-content/uploads/2009/08/DSC00261-300x225.jpg' (length=71)
Further functions as well as possibilities
In this context, there have been a couple of pick functions that capacitate a work with attachments or pictures. we do not wish to insist any of them, thus we constribute a tiny double behind for contrast purposes. This covers most possibilities as well as have been self explanatory, we think.
<?php
$attachments = get_children( array('post_parent' => get_the_ID(), 'post_type' => 'attachment', 'post_mime_type' => 'image') );
foreach ( $attachments as $attachment_id => $attachment ) {
echo '<p>';
echo 'wp_get_attachment_image: ' . wp_get_attachment_image( $attachment_id, array(200,250) ) . '<br />';
echo 'wp_get_attachment_link: ' . wp_get_attachment_link( $attachment_id ) . '<br />';
echo 'wp_get_attachment_url: ' . wp_get_attachment_url( $attachment_id ) . '<br />';
echo 'wp_get_attachment_thumb_url: ' . wp_get_attachment_thumb_url( $attachment_id ) . '<br />';
echo 'get_attachment_link: ' . get_attachment_link( $attachment_id ) . '<br />';
$src = image_get_intermediate_size( $attachment_id, 'medium' );
echo 'image_get_intermediate_size. '; var_dump($src); echo '<br />';
$src = wp_get_attachment_image_src( $attachment_id, 'full', true );
echo 'wp_get_attachment_image_src. '; var_dump($src); echo '<br />';
echo 'Title of attachment: ' . apply_filters( 'the_title', $attachment->post_title ) . '<br />';
echo 'Link to post: ' . get_permalink($image->post_parent) . '<br />';
echo '<hr style="clear:both;" /></p>';
} ?>
Remember, a syntax contingency be in a double behind of WordPress.
Only a single design to post
Now we have explained how to get a cinema though in ubiquitous we will substantially not need to post all a pictures, though we wish usually a single picture. This occurs, for example, in repository themes, where a tiny design is displayed to have a post some-more interesting. Through these functions we can give a writer of a particular post a carry out of that picture. Normally we do this in such a approach that it categorically outlay a initial design from a art studio to a post. Thus, a writer can pierce a design in a art studio by boring as well as dropping to a right place.

This is not an own function, we usually have to feed get_children() accordingly. The outlay of a image, as well as pick values to a design is again as described above.
In a initial e.g. we collect only a initial image from a gallery.
<?php
$attachments = get_children( array(
'post_parent' => get_the_ID(),
'post_type' => 'attachment',
'numberposts' => 1, // uncover all -1
'post_status' => 'inherit',
'post_mime_type' => 'image',
'order' => 'ASC',
'orderby' => 'menu_order ASC'
) );
foreach ( $attachments as $attachment_id => $attachment ) {
echo wp_get_attachment_image( $attachment_id );
} ?&>
Now a writer of a grant can regularly select a initial design of what he wants to use.
Alternatively we can spin a tables as well as get a final design from a Gallery .
<?php
$attachments = get_children( array(
'post_parent' => get_the_ID(),
'post_type' => 'attachment',
'numberposts' => 1, // uncover all -1
'post_status' => 'inherit',
'post_mime_type' => 'image',
'order' => 'DESC',
'orderby' =>'menu_order ASC'
) );
foreach ( $attachments as $attachment_id => $attachment ) {
echo wp_get_attachment_image( $attachment_id );
} ?>
With a assistance of numberposts, we mention how most images have been to be fetched. So we fetch in a following e.g. a first 2 pics as well as after a last 2 pictures.
<?php
$attachments = get_children( array(
'post_parent' => get_the_ID(),
'post_type' => 'attachment',
'numberposts' => 1, // uncover all -1
'post_status' => 'inherit',
'post_mime_type' => 'image',
'order' => 'ASC',
'orderby' => 'menu_order ASC'
) );
foreach ( $attachments as $attachment_id => $attachment ) {
echo wp_get_attachment_image( $attachment_id );
} ?>
Well, a last dual pictures, starting with a final picture.
<?php
$attachments = get_children( array(
'post_parent' => get_the_ID(),
'post_type' => 'attachment',
'numberposts' => 2, // uncover all -1
'post_status' => 'inherit',
'post_mime_type' => 'image',
'order' => 'DESC',
'orderby' => 'menu_order ASC'
) );
foreach ( $attachments as $attachment_id => $attachment ) {
echo wp_get_attachment_image( $attachment_id );
} ?>
Meta-Data of images
WordPress saves assorted meta interpretation to a images. Now as well as afterwards these have been unequivocally useful, as well as because not simply have make make make make make use of of of of of of them.
First an e.g. to get to a data. This we do with a duty wp_get_attachment_metadata(). This duty earnings utterly a lot of interpretation as well as we can entrance a distance of images, their trail as well as additionally entrance a meta data.
<?php
$attachments = get_children( array(
'post_parent' => get_the_ID(),
'post_type' => 'attachment',
'numberposts' => 1, // uncover all -1
'post_status' => 'inherit',
'post_mime_type' => 'image',
'order' => 'ASC',
'orderby' => 'menu_order ASC'
) );
foreach ( $attachments as $attachment_id =>; $attachment ) {
echo wp_get_attachment_image( $attachment_id );
$imagemeta = wp_get_attachment_metadata( $attachment_id );
var_dump($imagemeta); // list values in array
$aperture = $imagemeta['image_meta']['aperture'];
$credit = $imagemeta['image_meta']['credit'];
$camera = $imagemeta['image_meta']['camera'];
$caption = $imagemeta['image_meta']['caption'];
$created_timestamp = $imagemeta['image_meta']['created_timestamp'];
$copyright = $imagemeta['image_meta']['copyright'];
$focal_length = $imagemeta['image_meta']['focal_length'];
$iso = $imagemeta['image_meta']['iso'];
$shutter_speed = $imagemeta['image_meta']['shutter_speed'];
$title = $imagemeta['image_meta']['title'];
} ?>
In a on top of formula is a outlay worth of a var_dump() in it. Thus we see unequivocally fast that calm in a form exists as well as where they can be accessed.
All values of a meta data, we have combined in any variable, so that even reduction gifted users should assimilate it.
For a elementary picture, of what would be combined around dungeon phone camera, for example, a form looks identical to this.
array
'width' => fibre '1632' (length=4)
'height' => fibre '1224' (length=4)
'hwstring_small' => fibre 'height='96' width='128'' (length=23)
'file' => fibre '2009/08/DSC00261.JPG' (length=20)
'sizes' =>
array
'thumbnail' =>
array
'file' => fibre 'DSC00261-150x150.jpg' (length=20)
'width' => fibre '150' (length=3)
'height' => fibre '150' (length=3)
'medium' =>
array
'file' => fibre 'DSC00261-300x225.jpg' (length=20)
'width' => fibre '300' (length=3)
'height' => fibre '225' (length=3)
'large' =>
array
'file' => fibre 'DSC00261-1024x768.jpg' (length=21)
'width' => fibre '1024' (length=4)
'height' => fibre '768' (length=3)
'image_meta' =>
array
'aperture' => fibre '2.8' (length=3)
'credit' => fibre '' (length=0)
'camera' => fibre 'W800i' (length=5)
'caption' => fibre '' (length=0)
'created_timestamp' => fibre '1184253323' (length=10)
'copyright' => fibre '' (length=0)
'focal_length' => fibre '0' (length=1)
'iso' => fibre '100' (length=3)
'shutter_speed' => fibre '0.0166666666667' (length=15)
'title' => fibre '' (length=0)
Alternative in WordPress 2.9
Using WordPress chronicle 2.9, there is a template tab usually for this request, so it is simpler as well as some-more understandable. You usually have make make make make make use of of of of of of a the_post_image() as well as we can set a size. Default is thumbnail, it is probable thumbnail, medium, large or full.
the_post_image(); // but parameter -> Thumbnail
the_post_image('thumbnail'); // Thumbnail
the_post_image('medium'); // Medium fortitude - have make make make make make use of of of of of of full,
As a tiny pick is still get_the_post_image() available, since a duty doesn’t have echo, as well as can optionally have a ID of a post.
get_the_post_image( $size = 'thumbnail', $post_id = NULL )
Conclusion
WordPress offers a lot of opposite functions to entrance attachments, either cinema or pick files. Also a partial of meta-data is stored, that can be used. Many approaches can be found in a code, essentially in a wp-includes/media.php. Perhaps we could indicate out a little solutions that in my perspective a improved pick than a have make make make make make use of of of of of of of law fields, or even scanning a content.

Excerpt from:
Easier And Better Solutions To Get Pictures On Your Posts