Skip to content

Instantly share code, notes, and snippets.

@misterebs
Created September 29, 2016 04:12
Show Gist options
  • Save misterebs/23f9dfda7ffae8d773488e5fe7443a0b to your computer and use it in GitHub Desktop.
Save misterebs/23f9dfda7ffae8d773488e5fe7443a0b to your computer and use it in GitHub Desktop.
Display image from media library
//Embed Images
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;
}
function html5_insert_image($html, $id, $caption, $title, $align, $url) {
$html5 = "<figure id='post-$id media-$id' class='align-$align'>";
$html5 .= "<img src='$url' alt='$title' />";
if ($caption) {
$html5 .= "<figcaption>$caption</figcaption>";
}
$html5 .= "</figure>";
return $html5;
}
add_filter( 'image_send_to_editor', 'html5_insert_image', 10, 9 );
To display attacment in main post, insert this code in loop
<?php
if ( $thumbnail_id = get_post_thumbnail_id() ) {
if ( $image_src = wp_get_attachment_image_src( $thumbnail_id, 'normal-bg' ) )
printf( ' style="background-image: url(%s);"', $image_src[0] );
}
?>>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment