Skip to content

Instantly share code, notes, and snippets.

@davidchase
Last active December 12, 2015 07:09
Show Gist options
  • Save davidchase/4734938 to your computer and use it in GitHub Desktop.
Save davidchase/4734938 to your computer and use it in GitHub Desktop.
<?php
add_shortcode( 'slider_gallery', function( $atts ) {
$defaults = array(
'show_only' => 'slider',
'img_class' => 'theImage',
);
extract( shortcode_atts( $defaults, $atts ) );
$post_id = get_the_ID(); //use post id so this shortcode can be used on any page!
$images =& get_children( 'post_parent='.$post_id.'&order=ASC&orderby=menu_order&post_type=attachment&post_mime_type=image' );
if ( empty( $images ) ) :
return false;
else :
foreach ( $images as $attachment_id => $attachment ):
$title = $attachment->post_title;
$imageData = wp_get_attachment_url( $attachment_id );
preg_match( '/(.*?)('.$show_only.'.+ ?)/i', $imageData, $matches ); //match all files with 'slider' etc: slider_one.jpg or oneslider.jpg
$media[] = $matches[0];
$media = array_filter( $media );
endforeach;
foreach ( $media as $m ):
echo '<img class="'.$img_class.'" src="'.$m.'"/>';
endforeach;
endif;
return;
} );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment