Created
September 5, 2016 15:13
-
-
Save derweili/8acd7884e445f1ca5b6a5cb6b113ff15 to your computer and use it in GitHub Desktop.
WordPress Gallery based on Zurb Foundation Orbit Carousel
This file contains hidden or 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 | |
/** | |
Overwrite WordPress Gallery with Zurb Foundation Orbit Carousel | |
http://foundation.zurb.com/sites/docs/orbit.html | |
*/ | |
add_filter( 'post_gallery', 'my_custom_foundation_gallery', 10, 2 ); | |
function my_custom_foundation_gallery( $output, $attr ) { | |
global $post; | |
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 ]; | |
} | |
} | |
if ( empty( $attachments ) ) return ''; | |
// Here's your actual output, you may customize it to your need | |
$output = "<div class=\"orbit\" role=\"region\" aria-label=\"Favorite Space Pictures\" data-orbit>\n"; | |
$output .= "<ul class=\"orbit-container\">\n"; | |
$output .= "<button class=\"orbit-previous\"><span class=\"show-for-sr\">Previous Slide</span>◀︎</button>"; | |
$output .= "<button class=\"orbit-next\"><span class=\"show-for-sr\">Next Slide</span>▶︎</button>"; | |
// Now you loop through each attachment | |
$x = 1; | |
foreach ( $attachments as $id => $attachment ) { | |
// Fetch all data related to attachment | |
$img = wp_prepare_attachment_for_js($id); | |
// If you want a different size change 'large' to eg. 'medium' | |
$url = $img[ "sizes" ][ "large" ][ "url" ]; | |
$height = $img[ "sizes" ][ "large" ][ "height" ]; | |
$width = $img[ "sizes" ][ "large" ][ "width" ]; | |
$alt = $img[ "alt" ]; | |
// Store the caption | |
$caption = $img[ "caption" ]; | |
$output .= "<li class=\"orbit-slide\">\n"; | |
$output .= "<img src=\"{$url}\" width=\"{$width}\" height=\"{$height}\" alt=\"{$alt}\" />\n"; | |
// Output the caption if it exists | |
if ($caption) { | |
$output .= "<figcaption class=\"orbit-caption\">{$caption}</figcaption>\n"; | |
} | |
$output .= "</li>\n"; | |
$x++; | |
} | |
$output .= "</ul>\n"; | |
$output .= "</div>\n"; | |
return $output; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment