Skip to content

Instantly share code, notes, and snippets.

@pablonoel
Created September 8, 2017 00:33
Show Gist options
  • Save pablonoel/b7d0d822f9f2fdfe14dfffa915917e66 to your computer and use it in GitHub Desktop.
Save pablonoel/b7d0d822f9f2fdfe14dfffa915917e66 to your computer and use it in GitHub Desktop.
A simplification for Wordpress gallery shortcode output
<?php
// Thumbnails
add_theme_support( 'post-thumbnails' );
add_image_size( 'image_1024', 1024, 1024 );
// Gallery
function simplier_gallery($attr) {
$post = get_post();
static $instance = 0;
$instance++;
if ( ! empty( $attr['ids'] ) ) {
if ( empty( $attr['orderby'] ) )
$attr['orderby'] = 'post__in';
$attr['include'] = $attr['ids'];
}
$output = apply_filters('post_gallery', '', $attr);
if ( $output != '' )
return $output;
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' => 'li',
'icontag' => '',
'captiontag' => '',
'columns' => 3,
'size' => 'image_1024',
'zoomsize' => 'large',
'include' => '',
'exclude' => ''
), $attr));
$id = intval($id);
if ( 'RAND' == $order )
$orderby = 'none';
if ( !empty($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) ) {
$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;
}
$output = "<ul class='gallery'>";
$output .= "";
foreach ( $attachments as $id => $attachment ) {
$src = wp_get_attachment_image_src($id, $size);
$zoom = wp_get_attachment_image_src($id, $zoomsize);
$link = get_attachment_link($id);
$title = $attachment->post_title;
$caption = $attachment->post_excerpt;
$description = $attachment->post_content;
$ratio = 'regular';
if ($src[1] == '1024'){
$ratio = 'landscape';
}
if ($src[2] == '1024'){
$ratio = 'portrait';
}
$itemtag = 'li id="image_'.$id.'" class="'.$ratio.'"';
if ($caption=='') {
$imagetag = '<figure><img src="'.$src[0].'" alt="'.$title.'" /></figure>';
}else {
$imagetag = '<figure><img src="'.$src[0].'" alt="'.$title.'" /><figcaption>'.$caption.'</figcaption></figure>';
}
$output .= "<$itemtag>";
$output .= "\n\t\t\t";
$output .= "$imagetag";
$output .= "\n\t\t";
$output .= "</$itemtag>";
$output .= "\n\t\t";
}
$output .= "</ul>";
return $output;
}
add_shortcode('gallery', 'simplier_gallery');
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment