Merge new gallery
Based on Roots Sage Gallery: https://github.com/roots/sage/blob/5b9786b8ceecfe717db55666efe5bcf0c9e18 01c/lib/gallery.php
This commit is contained in:
parent
0c78cccb4e
commit
ac5a88cce4
|
@ -1,129 +1,105 @@
|
|||
<?php
|
||||
//Based on Jeff Hays code and his article here: http://robido.com/wordpress/wordpress-gallery-filter-to-modify-the-html-output-of-the-default-gallery-shortcode-and-style/
|
||||
// Custom filter function to modify default gallery shortcode output
|
||||
function bootstrap_wp_gallery( $output, $attr ) {
|
||||
//Based on Roots Sage Gallery: https://github.com/roots/sage/blob/5b9786b8ceecfe717db55666efe5bcf0c9e1801c/lib/gallery.php
|
||||
|
||||
// Remove built in shortcode
|
||||
remove_shortcode('gallery', 'gallery_shortcode');
|
||||
|
||||
// Initialize
|
||||
global $post, $wp_locale;
|
||||
|
||||
// Gallery instance counter
|
||||
// Replace with custom shortcode
|
||||
function shortcode_gallery($attr) {
|
||||
$post = get_post();
|
||||
static $instance = 0;
|
||||
$instance++;
|
||||
|
||||
// Validate the author's orderby attribute
|
||||
if ( isset( $attr['orderby'] ) ) {
|
||||
$attr['orderby'] = sanitize_sql_orderby( $attr['orderby'] );
|
||||
if ( ! $attr['orderby'] ) unset( $attr['orderby'] );
|
||||
if (!empty($attr['ids'])) {
|
||||
if (empty($attr['orderby'])) {
|
||||
$attr['orderby'] = 'post__in';
|
||||
}
|
||||
|
||||
// Get attributes from shortcode
|
||||
extract( shortcode_atts( array(
|
||||
$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([
|
||||
'order' => 'ASC',
|
||||
'orderby' => 'menu_order ID',
|
||||
'id' => $post->ID,
|
||||
'itemtag' => 'div',
|
||||
'icontag' => 'div',
|
||||
'captiontag' => 'div',
|
||||
'itemtag' => '',
|
||||
'icontag' => '',
|
||||
'captiontag' => '',
|
||||
'columns' => 3,
|
||||
'size' => 'thumbnail',
|
||||
'exclude' => ''
|
||||
), $attr ) );
|
||||
|
||||
// Initialize
|
||||
$id = intval( $id );
|
||||
$attachments = array();
|
||||
if ( $order == 'RAND' ) $orderby = 'none';
|
||||
|
||||
|
||||
if ( ! empty( $include ) ) {
|
||||
|
||||
// Include attribute is present
|
||||
$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 ) );
|
||||
|
||||
// Setup attachments array
|
||||
foreach ( $_attachments as $key => $val ) {
|
||||
$attachments[ $val->ID ] = $_attachments[ $key ];
|
||||
'include' => '',
|
||||
'exclude' => '',
|
||||
'link' => ''
|
||||
], $attr));
|
||||
$id = intval($id);
|
||||
$columns = (12 % $columns == 0) ? $columns : 3;
|
||||
$grid = sprintf('col-sm-%1$s col-lg-%1$s', 12 / $columns);
|
||||
if ($order === 'RAND') {
|
||||
$orderby = 'none';
|
||||
}
|
||||
|
||||
} else if ( ! empty( $exclude ) ) {
|
||||
|
||||
// Exclude attribute is present
|
||||
$exclude = preg_replace( '/[^0-9,]+/', '', $exclude );
|
||||
|
||||
// Setup attachments array
|
||||
$attachments = get_children( array( 'post_parent' => $id, 'exclude' => $exclude, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby ) );
|
||||
if (!empty($include)) {
|
||||
$_attachments = get_posts(['include' => $include, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby]);
|
||||
$attachments = [];
|
||||
foreach ($_attachments as $key => $val) {
|
||||
$attachments[$val->ID] = $_attachments[$key];
|
||||
}
|
||||
} elseif (!empty($exclude)) {
|
||||
$attachments = get_children(['post_parent' => $id, 'exclude' => $exclude, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby]);
|
||||
} else {
|
||||
// Setup attachments array
|
||||
$attachments = get_children( array( 'post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby ) );
|
||||
$attachments = get_children(['post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby]);
|
||||
}
|
||||
|
||||
if ( empty( $attachments ) ) return '';
|
||||
|
||||
// Filter gallery differently for feeds
|
||||
if ( is_feed() ) {
|
||||
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";
|
||||
foreach ($attachments as $att_id => $attachment) {
|
||||
$output .= wp_get_attachment_link($att_id, $size, true) . "\n";
|
||||
}
|
||||
return $output;
|
||||
}
|
||||
|
||||
// Filter tags and attributes
|
||||
$itemtag = tag_escape( $itemtag );
|
||||
$captiontag = tag_escape( $captiontag );
|
||||
$columns = intval( $columns );
|
||||
$itemwidth = $columns > 0 ? floor( 12 / $columns ) : 100;
|
||||
$float = is_rtl() ? 'right' : 'left';
|
||||
$selector = "gallery-{$instance}";
|
||||
|
||||
// Filter gallery CSS
|
||||
$output = apply_filters( 'gallery_style', "
|
||||
|
||||
<div class='gallery galleryid-{$id} row' id='$selector'>"
|
||||
);
|
||||
|
||||
// Iterate through the attachments in this gallery instance
|
||||
$unique = (get_query_var('page')) ? $instance . '-p' . get_query_var('page') : $instance;
|
||||
$output = '<div class="gallery gallery-' . $id . '-' . $unique . '">';
|
||||
$i = 0;
|
||||
foreach ( $attachments as $id => $attachment ) {
|
||||
|
||||
// Attachment link
|
||||
$link = isset( $attr['link'] ) && 'file' == $attr['link'] ? wp_get_attachment_link( $id, $size, false, false ) : wp_get_attachment_link( $id, $size, true, false );
|
||||
|
||||
// Start itemtag
|
||||
$output .= "<{$itemtag} class='gallery-item col-md-{$itemwidth}'>";
|
||||
|
||||
// icontag
|
||||
$output .= "
|
||||
<{$icontag} class='gallery-icon'>
|
||||
$link
|
||||
</{$icontag}>";
|
||||
|
||||
if ( $captiontag && trim( $attachment->post_excerpt ) ) {
|
||||
|
||||
// captiontag
|
||||
$output .= "
|
||||
<{$captiontag} class='gallery-caption'>
|
||||
" . wptexturize($attachment->post_excerpt) . "
|
||||
</{$captiontag}>";
|
||||
|
||||
foreach ($attachments as $id => $attachment) {
|
||||
switch($link) {
|
||||
case 'file':
|
||||
$image = wp_get_attachment_link($id, $size, false, false);
|
||||
break;
|
||||
case 'none':
|
||||
$image = wp_get_attachment_image($id, $size, false, ['class' => 'thumbnail img-thumbnail']);
|
||||
break;
|
||||
default:
|
||||
$image = wp_get_attachment_link($id, $size, true, false);
|
||||
break;
|
||||
}
|
||||
|
||||
// End itemtag
|
||||
$output .= "</{$itemtag}>";
|
||||
|
||||
// Line breaks by columns set
|
||||
if($columns > 0 && ++$i % $columns == 0) $output .= '<br style="clear: both">';
|
||||
|
||||
$output .= ($i % $columns == 0) ? '<div class="row gallery-row">' : '';
|
||||
$output .= '<div class="' . $grid .'">' . $image;
|
||||
if (trim($attachment->post_excerpt)) {
|
||||
$output .= '<div class="caption hidden">' . wptexturize($attachment->post_excerpt) . '</div>';
|
||||
}
|
||||
|
||||
// End gallery output
|
||||
$output .= "
|
||||
<br style='clear: both;'>
|
||||
</div>\n";
|
||||
|
||||
$output .= '</div>';
|
||||
$i++;
|
||||
$output .= ($i % $columns == 0) ? '</div>' : '';
|
||||
}
|
||||
$output .= ($i % $columns != 0 ) ? '</div>' : '';
|
||||
$output .= '</div>';
|
||||
return $output;
|
||||
|
||||
}
|
||||
add_shortcode('gallery', 'shortcode_gallery');
|
||||
|
||||
// Apply filter to default gallery shortcode
|
||||
add_filter( 'post_gallery', 'bootstrap_wp_gallery', 10, 2 );
|
||||
/**
|
||||
* Add class="thumbnail img-thumbnail" to attachment items
|
||||
*/
|
||||
function attachment_link_class($html) {
|
||||
$html = str_replace('<a', '<a class="thumbnail img-thumbnail"', $html);
|
||||
return $html;
|
||||
}
|
||||
add_filter('wp_get_attachment_link', 'attachment_link_class', 10, 1);
|
||||
|
|
Reference in New Issue