Skip to content

Instantly share code, notes, and snippets.

@soggybag
Created November 28, 2015 06:07
Show Gist options
  • Save soggybag/4d49a224f1cf089ef1d0 to your computer and use it in GitHub Desktop.
Save soggybag/4d49a224f1cf089ef1d0 to your computer and use it in GitHub Desktop.
WordPress - get images in current post with short code
// Use this in the content of a post or page.
// [get_images size="thumbnail" num=3]
// The code below is added to your functions.php
function get_images_func( $atts ) {
global $post;
$num = $atts["num"];
$size = $atts["size"];
$image_html = ""; // Hello World $size";
$attachments = get_posts( array("numberposts" => $num,
"post_type" => "attachment",
"post_parent" => $post->ID,
"orderby" => "name" ) );
foreach( $attachments as $attachment ) {
$img = wp_get_attachment_image( $attachment->ID, $size );
$image_html .= "<div class='post-img grid-20'>$img</div>";
}
return $image_html;
}
function register_shortcodes(){
add_shortcode( 'get-images', 'get_images_func' );
}
add_action( 'init', 'register_shortcodes');
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment