Skip to content

Instantly share code, notes, and snippets.

@ms-studio
Created October 21, 2012 22:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ms-studio/3928765 to your computer and use it in GitHub Desktop.
Save ms-studio/3928765 to your computer and use it in GitHub Desktop.
WP image toolbox array
// paste this function into your functions.php file
function image_toolbox_array($size = 'thumbnail', $howmany = -1) {
if ( $images = get_children ( array (
'post_parent' => get_the_ID(),
'post_type' => 'attachment',
'numberposts' => $howmany , // -1 = show all
'post_status' => null,
'post_mime_type' => 'image',
'orderby' => 'menu_order',
'order' => 'ASC',
// 'linked' => $link, // link = create link
))) {
// init the array!
$img_gallery_array = array();
foreach ( $images as $image ) {
$img_id = $image->ID;
// we will use that a few times
$img_custom_size = image_downsize( $img_id, $size );
$img_large_size = wp_get_attachment_image_src( $img_id, 'large' );
$img_full_size = wp_get_attachment_image_src( $img_id, 'full' );
// put everything into the array...
$img_gallery_array[] = array(
"id" => $img_id,
"url-custom" => $img_custom_size[0],
"url-large" => $img_large_size[0],
"url-full" => $img_full_size[0],
// DIMENSIONS
"width-custom" => $img_custom_size[1],
"height-custom" => $img_custom_size[2],
// defaults
"width-large" => $img_large_size[1],
"height-large" => $img_large_size[2],
"width-full" => $img_full_size[1],
"height-full" => $img_full_size[2],
// TEXT
"title" => apply_filters('the_title',$image->post_title),
"caption" => $image->post_excerpt,
"description" => $image->post_content,
);
} // end foreach
return $img_gallery_array;
}
}
// an example:
// build image gallery
$img_info = image_toolbox_array('horiz-thumb',-1);
// note: horiz-thumb is a custom format; -1 means we want all images.
if (!empty($img_info)) {
// how many images do we have?
$img_gallery_items = count($img_info);
foreach ($img_info as $key => $row){
echo '<img src="'.$img_info[$key]["url-custom"].'" data-original="'.$img_info[$key]["url-large"].'" id="img-'.$img_info[$key]["id"].'" class="img-large lazy-load" />';
} // end foreach
} // end if
@ms-studio
Copy link
Author

Testing in WP (3.6-alpha-23486) : line 44 - apply_filters('the_title',$image->post_title) - triggers the following message:

Warning: Missing argument 2 for _post_formats_title() in /wp-includes/post-formats.php on line 952

@ms-studio
Copy link
Author

Update: the issue is fixed, see http://core.trac.wordpress.org/ticket/24233

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment