Skip to content

Instantly share code, notes, and snippets.

@imjjss
Created May 29, 2012 14:36
Show Gist options
  • Save imjjss/2828780 to your computer and use it in GitHub Desktop.
Save imjjss/2828780 to your computer and use it in GitHub Desktop.
gallery fix
// Registers our function to filter default gallery shortcode
remove_shortcode('gallery');
add_shortcode('gallery', 'sandbox_gallery');
// Function to filter the default gallery shortcode
function sandbox_gallery($attr) {
global $post;
static $instance = 0;
$instance++;
// We're trusting author input, so let's at least make sure it looks like a valid orderby statement
if ( isset( $attr['orderby'] ) ) {
$attr['orderby'] = sanitize_sql_orderby( $attr['orderby'] );
if ( !$attr['orderby'] )
unset( $attr['orderby'] );
}
extract(shortcode_atts(array(
'order' => 'ASC',
'orderby' => 'menu_order ID',
'id' => $post->ID,
'itemtag' => 'dl',
'icontag' => 'dt',
'captiontag' => 'dd',
'columns' => 3,
'size' => 'thumbnail',
'include' => '',
'exclude' => ''
), $attr));
$id = intval($id);
if ( 'RAND' == $order )
$orderby = 'none';
if ( !empty($include) ) {
$include = preg_replace( '/[^0-9,]+/', '', $include );
$_attachments = get_posts( array('include' => $include, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
$attachments = array();
foreach ( $_attachments as $key => $val ) {
$attachments[$val->ID] = $_attachments[$key];
}
} elseif ( !empty($exclude) ) {
$exclude = preg_replace( '/[^0-9,]+/', '', $exclude );
$attachments = get_children( array('post_parent' => $id, 'exclude' => $exclude, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
} else {
$attachments = get_children( array('post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
}
if ( empty($attachments) )
return '';
if ( is_feed() ) {
$output = "\n";
foreach ( $attachments as $att_id => $attachment )
$output .= wp_get_attachment_link($att_id, $size, true) . "\n";
return $output;
}
// check to see if tags have been set to false. If they are the defaults or have been set to a string value use that as the tag.
if ($itemtag) $itemtag = tag_escape($itemtag);
if ($captiontag) $captiontag = tag_escape($captiontag);
if ($icontag) $icontag = tag_escape($icontag);
$columns = intval($columns);
$selector = "gallery-{$instance}";
$output = "<div id='$selector' class='gallery galleryid-{$id}'>\n";
$i = 0;
foreach ( $attachments as $id => $attachment ) {
++$i;
$link = isset($attr['link']) && 'file' == $attr['link'] ? wp_get_attachment_link($id, $size, false, false) : wp_get_attachment_link($id, $size, true, false);
if ($itemtag) {
$output .= '<'.$itemtag.' class="gallery-item ';
if( $columns > 0 && $i % $columns == 0 ) $output .= " last";
$output .= '">';
}
if ($icontag) $output .= "\n\t<" .$icontag. ">\t";
$output .= "\n\t".$link;
if ($icontag) $output .= "\n\t</".$icontag.">";
// if the attachment has a caption set
if ( trim($attachment->post_excerpt) ) {
if ($captiontag) $output .= "\n<" .$captiontag. ">\n\t";
$output .= wptexturize($attachment->post_excerpt);
if ($captiontag) $output .= "\n</" .$captiontag. ">" . "<!-- end caption -->\n";
}
if ($itemtag) $output .= "\n</".$itemtag ."><!-- end itemtag -->\n";
if ( $columns > 0 && $i % $columns == 0 ) $output .= "\n";
}
$output .= "</div><!-- end gallery -->\n";
return $output;
}
@imjjss
Copy link
Author

imjjss commented May 29, 2012

.gallery {
margin: auto;
/* gallery clearing*/
overflow: hidden;
width: 100%;
}
.gallery .gallery-item {
float: left;
margin-top: 10px;
text-align: center;
}
.gallery img {
border: 2px solid #cfcfcf;
}
.gallery .gallery-caption {
margin-left: 0;
}
.gallery br { clear: both }
/* available Columns */
.col-2 { width: 50% }
.col-3 { width: 33.333% }
.col-4 { width: 25% }
.col-5 { width: 20% }
.col-6 { width: 16.666% }
.col-7 { width: 14.285% }
.col-8 { width: 12.5% }
.col-9 { width: 11.111% }

@imjjss
Copy link
Author

imjjss commented May 29, 2012

refind for more feature:
[gallery link="file" columns="3" itemtag="div" icontag="" captiontag="p"]

//the own renamed function
function wpe_gallery_shortcode($attr) {
global $post;

static $instance = 0;
$instance++;

// Allow plugins/themes to override the default gallery template.
$output = apply_filters('post_gallery', '', $attr);
if ( $output != '' )
return $output;

// We're trusting author input, so let's at least make sure it looks like a valid orderby statement
if ( isset( $attr['orderby'] ) ) {
$attr['orderby'] = sanitize_sql_orderby( $attr['orderby'] );
if ( !$attr['orderby'] )
unset( $attr['orderby'] );
}

extract(shortcode_atts(array(
'order' => 'ASC',
'orderby' => 'menu_order ID',
'id' => $post->ID,
'itemtag' => 'dl',
'icontag' => 'dt',
'captiontag' => 'dd',
'columns' => 3,
'size' => 'thumbnail',
'include' => '',
'exclude' => ''
), $attr));

$id = intval($id);
if ( 'RAND' == $order )
$orderby = 'none';

if ( !empty($include) ) {
$include = preg_replace( '/[^0-9,]+/', '', $include );
$_attachments = get_posts( array('include' => $include, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );

$attachments = array();
foreach ( $_attachments as $key => $val ) {
$attachments[$val->ID] = $_attachments[$key];
}
} elseif ( !empty($exclude) ) {
$exclude = preg_replace( '/[^0-9,]+/', '', $exclude );
$attachments = get_children( array('post_parent' => $id, 'exclude' => $exclude, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
} else {
$attachments = get_children( array('post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby) );
}

if ( empty($attachments) )
return '';

if ( is_feed() ) {
$output = "\n";
foreach ( $attachments as $att_id => $attachment )
$output .= wp_get_attachment_link($att_id, $size, true) . "\n";
return $output;
}

// check to see if tags have been set to false. If they are the defaults or have been set to a string value use that as the tag.
if ($itemtag) $itemtag = tag_escape($itemtag);
if ($captiontag) $captiontag = tag_escape($captiontag);
if ($icontag) $icontag = tag_escape($icontag);
$columns = intval($columns);

$selector = "gallery-{$instance}";

$output = apply_filters('gallery_style', "\n");

$i = 0;
foreach ( $attachments as $id => $attachment ) {
++$i;
$link = isset($attr['link']) && 'file' == $attr['link'] ? wp_get_attachment_link($id, $size, false, false) : wp_get_attachment_link($id, $size, true, false);

if ($itemtag) {
$output .= "\t 0 && $i % $columns == 0) $output .= "last";
$output .= ''>';
}
if ($icontag) $output .= "";
$output .= "\n\t\t" . $link . "\n";
if ($icontag) $output .= "";
// if the attachment has a caption set
if ( trim($attachment->post_excerpt) ) {
if ($captiontag) $output .= "\t\t";
$output .= wptexturize($attachment->post_excerpt);
if ($captiontag) $output .= "" . "\n";
}
if ($itemtag) $output .= "\t" . "\n";
if ( $columns > 0 && $i % $columns == 0 ) $output .= "\n";
}

$output .= "\n";

return $output;
}

oh, here's the css as well:
gallery { margin: 0 24px; width: 552px; float: left;}
.gallery-item { float: left; margin: 0 24px 24px 0; background:#C4BFA2; }
.gallery .last { margin-right: 0; }
.gallery-icon {}
.gallery-caption {}
.gallery br { clear: both; }

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