Richard
Richard

Reputation: 778

Get a single specific image from Wordpress Media Library

I've uploaded images to Wordpress Media Library.

I understand that I can view am image then get the URL for that specific image and then use the img html tag to display this on the page.

This however doesn't get the alt, title, caption and description of the image.

The img is not attached to a post or page field and so i assume you cannot use the Get Attachment function etc.

The reason I want to use a function instead of writing out a static img html code is so that they are cached better and easier to maintain with all data for the image been updated in the Media Library instead of having to edit html code which is not idea for the end user.

thank you in advance.

Upvotes: 17

Views: 55427

Answers (4)

luukvhoudt
luukvhoudt

Reputation: 1992

So many complicated and to my opinion wrong answers while the answer is very straight forward:

<?php
$url_to_my_attachment = "http://example.com/wp-content/uploads/image.png";

$attachment_id = attachment_url_to_postid($url_to_my_attachment);

print wp_get_attachment_image($attachment_id);

For more information have a look at wp_get_attachment_image

Note: this renders a "responsive image" where the alt attribute contains the alt data of the attachment. Thus this answer doesn't completely satisfy the OP's request which also demands to include the title, description and caption fields. See the accepted answer or other answers on how to include these other fields.

Upvotes: 2

stalinrajindian
stalinrajindian

Reputation: 1431

Please try to below code:

<?php
      $attachmentID = 1875;
      $imageSizeName = "thumbnail";
      $img = wp_get_attachment_image_src($attachmentID, $imageSizeName);
      //print_r($img);
?>

<img src="<?php echo $img[0]; ?>" alt="image">

Upvotes: 4

Behnam
Behnam

Reputation: 6459

first get image

function get_images_from_media_library() {
    $args = array(
        'post_type' => 'attachment',
        'post_mime_type' =>'image',
        'post_status' => 'inherit',
        'posts_per_page' => 5,
        'orderby' => 'rand'
    );
    $query_images = new WP_Query( $args );
    $images = array();
    foreach ( $query_images->posts as $image) {
        $images[]= $image->guid;
    }
    return $images;
}

and display image

function display_images_from_media_library() {

    $imgs = get_images_from_media_library();
    $html = '<div id="media-gallery">';

    foreach($imgs as $img) {

        $html .= '<img src="' . $img . '" alt="" />';

    }

    $html .= '</div>';

    return $html;

}

and use php fire event

<?php echo display_images_from_media_library(); ?>

or use this function

<?php

if ( $attachments = get_children( array(
'post_type' => 'attachment',
'post_mime_type'=>'image',
'numberposts' => 1,
'post_status' => null,
'post_parent' => $post->ID
)));
foreach ($attachments as $attachment) {
echo wp_get_attachment_link( $attachment->ID, '' , true, false, 'Link to image attachment' );
}

?>

Upvotes: 15

Matt.C
Matt.C

Reputation: 1322

I presume you have an attachment ID? Have you tried using attachement functions?

From the codex:

Note that media items are also 'Posts' in their own right and can be displayed as such via the WordPress Template Hierarchy. Themes can make use of this to loop over media items or create galleries.

The following functions should get you started:

you can retrieve the image src using: wp_get_attachment_image_src()

$img= wp_get_attachment_image_src($attachmentID, $imageSizeName); 

you can get the image caption using: get_post_field()

get_post_field('post_excerpt', $attachmentID)

you can get the alt tag using: get_post_meta()

get_post_meta($attachmentID, '_wp_attachment_image_alt', true);

Upvotes: 21

Related Questions