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.
Open a page or post where the image is published and locate the image. After that, right-click on the image, and select 'Open image in new tab' option. Once the image opens in the new tab, look at the URL in the address bar. That is your WordPress image URL.
Now the issue i think is the way how wordpress media library works with multiple image selection. when selecting multiple images it force the user to click shift/ctrl and image click to select all needed images (which is ok for desktop).
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' );
}
?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With