Last active
August 29, 2015 14:06
-
-
Save webinista/4bfad9e1405a17512ae3 to your computer and use it in GitHub Desktop.
Rewrite WordPress' gallery shortcode output to use markup that's FlexSlider friendly.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* | |
Add this code to functions.php. Then add Woothemes' FlexSlider to your templates | |
as you otherwise would. | |
Rewrite 29 September 2014 to use WordPress filters instead of overwriting the | |
shortcode handler. | |
*/ | |
function build_gallery_content( $attrs ){ | |
static $instance = 0; | |
$instance++; | |
/* | |
Limiting what the user can do by | |
locking down most short code options. | |
*/ | |
extract(shortcode_atts(array( | |
'id' => $post->ID, | |
'include' => '', | |
'exclude' => '' | |
), $attrs)); | |
$id = intval($id); | |
if ( !empty($include) ) { | |
$params = array( | |
'include' => $include, | |
'post_status' => 'inherit', | |
'post_type' => 'attachment', | |
'post_mime_type' => 'image', | |
'order' => 'ASC', | |
'orderby' => 'menu_order ID'); | |
$_attachments = get_posts( $params ); | |
$attachments = array(); | |
foreach ( $_attachments as $key => $val ) { | |
$attachments[$val->ID] = $_attachments[$key]; | |
} | |
} elseif ( !empty($exclude) ) { | |
$params = array( | |
'post_parent' => $id, | |
'exclude' => $exclude, | |
'post_status' => 'inherit', | |
'post_type' => 'attachment', | |
'post_mime_type' => 'image', | |
'order' => 'ASC', | |
'orderby' => 'menu_order ID'); | |
$attachments = get_children( $params ); | |
} else { | |
$params = array( | |
'post_parent' => $id, | |
'post_status' => 'inherit', | |
'post_type' => 'attachment', | |
'post_mime_type' => 'image', | |
'order' => 'ASC', | |
'orderby' => 'menu_order ID'); | |
$attachments = get_children( $params ); | |
} | |
if ( empty($attachments) ) | |
return ''; | |
$selector = "gallery-{$instance}"; | |
$gallery_div = sprintf("<div class='flexslider'><ul id='%s' class='slides gallery galleryid-%d gallery-columns-1 gallery-size-full'>", $selector, $id); | |
$output = $gallery_div; | |
foreach ( $attachments as $id => $attachment ) { | |
/* | |
Use wp_get_attachment_link to return a photo + link | |
to attachment page or image | |
http://codex.wordpress.org/Function_Reference/wp_get_attachment_link | |
*/ | |
$img = wp_get_attachment_image( $id, 'full', false); | |
$caption = ''; | |
/* | |
Set the caption string if there is one. | |
*/ | |
if( $captiontag && trim($attachment->post_excerpt) ){ | |
$caption = sprintf("\n\t<figcaption class='wp-caption-text gallery-caption'>\n\t<div>\n%s\n\t</div>\n\t</figcaption>", wptexturize($attachment->post_excerpt)); | |
} | |
/* | |
Set the output for each slide. | |
*/ | |
$output .= sprintf("<li class='gallery-item'><figure class='gallery-icon'>%s\n\t%s</figure></li>", $img, $caption); | |
} | |
$output .= '</ul>\n</div>'; | |
return $output; | |
} | |
function custom_gallery_shortcode( $output = '', $attrs){ | |
$return = $output; | |
# Gallery function that returns new markup. | |
$gallery = build_gallery_content( $attrs ); | |
if( !empty( $gallery ) ) { | |
$return = $gallery; | |
} | |
return $return; | |
} | |
add_filter( 'post_gallery', 'custom_gallery_shortcode', 10, 2); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment