Created
November 28, 2015 06:07
-
-
Save soggybag/4d49a224f1cf089ef1d0 to your computer and use it in GitHub Desktop.
WordPress - get images in current post with short code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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