Skip to content

Instantly share code, notes, and snippets.

@khoipro
Created August 14, 2017 08:19
Show Gist options
  • Save khoipro/e2b8b84d9dc4c019a84323be4d1a80f6 to your computer and use it in GitHub Desktop.
Save khoipro/e2b8b84d9dc4c019a84323be4d1a80f6 to your computer and use it in GitHub Desktop.
Gallery Shortcode WordPress - Hacking Output
<?php
/*
* Change WordPress default gallery output
*/
add_filter('post_gallery', 'ct_post_gallery', 10, 2);
function ct_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' => 'div',
'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 '';
if(is_page_template('templates/partners.php')) {
foreach ($attachments as $id => $attachment) {
$image_alt = $attachment->post_title;
$link = $attachment->post_content;
$img = wp_get_attachment_image_src($id, 'full');
$output .= "<$icontag class=\"col-md-2 col-sm-4 col-xs-6 text-center item\">\n";
$output .= "<a href=\"$link\" target=\"_blank\" rel=\"nofollow\">";
$output .= "<img src=\"{$img[0]}\" alt=\"$image_alt\" />\n";
$output .= "</a>";
$output .= "</$icontag>\n";
}
} elseif(is_home()) {
foreach ($attachments as $id => $attachment) {
$image_alt = $attachment->post_title;
$link = $attachment->post_content;
$img = wp_get_attachment_image_src($id, 'full');
$output .= "<$icontag>\n";
$output .= "<img src=\"{$img[0]}\" alt=\"$image_alt\" />\n";
$output .= "</$icontag>\n";
}
} elseif(is_singular('album')) {
foreach ($attachments as $id => $attachment) {
$image_alt = $attachment->post_title;
$img = wp_get_attachment_image_src($id, 'large');
$output .= "<$icontag>\n";
$output .= "<img src=\"{$img[0]}\" alt=\"$image_alt\" />\n";
$output .= "</$icontag>\n";
}
} else {
foreach ($attachments as $id => $attachment) {
$image_alt = $attachment->post_title;
$img = wp_get_attachment_image_src($id, 'large');
$output .= "<$icontag>\n";
$output .= "<img src=\"{$img[0]}\" alt=\"$image_alt\" />\n";
$output .= "</$icontag>\n";
}
}
return $output;
}
;?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment