Created
January 6, 2017 09:48
-
-
Save hsleonis/afbf163f80921e3601dd48f1101050ae to your computer and use it in GitHub Desktop.
WP change default post gallery output
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 | |
add_filter('post_gallery', 'my_post_gallery', 10, 2); | |
function my_post_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=\"slideshow-wrapper\">\n"; | |
$output .= "<div class=\"preloader\"></div>\n"; | |
$output .= "<ul data-orbit>\n"; | |
// Now you loop through each attachment | |
foreach ($attachments as $id => $attachment) { | |
// Fetch the thumbnail (or full image, it's up to you) | |
// $img = wp_get_attachment_image_src($id, 'medium'); | |
// $img = wp_get_attachment_image_src($id, 'my-custom-image-size'); | |
$img = wp_get_attachment_image_src($id, 'full'); | |
$output .= "<li>\n"; | |
$output .= "<img src=\"{$img[0]}\" width=\"{$img[1]}\" height=\"{$img[2]}\" alt=\"\" />\n"; | |
$output .= "</li>\n"; | |
} | |
$output .= "</ul>\n"; | |
$output .= "</div>\n"; | |
return $output; | |
} |
Using attachment data like: title, caption etc.
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));
}
if (empty($_attachments)) return '';
$output = "<div class=\"gallery\">\n";
foreach ($_attachments as $attachment) {
// Fetch the thumbnail (or full image, it's up to you)
// $img = wp_get_attachment_image_src($id, 'medium');
// $img = wp_get_attachment_image_src($id, 'my-custom-image-size');
$img = wp_get_attachment_image_src($attachment->ID, 'full');
$output .= "<div class='col-sm-3'>\n";
$output .= "<a rel='prettyphoto[pp_gal]' href=\"{$img[0]}\" title=\"STYLE: {$attachment->post_title}\"><img class='img-responsive' src=\"{$img[0]}\" width=\"{$img[1]}\" height=\"{$img[2]}\" alt=\"\" /></a>\n";
$output .= "</div>\n";
}
$output .= "</div>\n";
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Using slick slider: