Created
March 8, 2012 18:32
-
-
Save yoosuf/2002547 to your computer and use it in GitHub Desktop.
WordPress gallery Hack, Make your own styling and add your own CSS, JavaScript and spice it up
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 | |
/* | |
I grabbed this form WordPress Galleria Plugin, I prefer using this in functions.php rather than a plugin | |
*/ | |
remove_shortcode('gallery'); | |
add_shortcode('gallery', 'photo_gallery_shortcode'); | |
function photo_gallery_shortcode($attr) { | |
global $post; | |
$pid = $post->ID; | |
$image_size = "500px"; | |
// We're trusting author input, so let's at least make sure it looks like a valid orderby statement | |
if ( isset( $attr['orderby'] ) ) { | |
$attr['orderby'] = sanitize_sql_orderby( $attr['orderby'] ); | |
if ( !$attr['orderby'] ) | |
unset( $attr['orderby'] ); | |
} | |
extract(shortcode_atts(array( | |
'orderby' => 'menu_order ASC, ID ASC', | |
'id' => $post->ID, | |
'size' => $image_size, | |
), $attr)); | |
$id = intval($id); | |
$attachments = get_children("post_parent=$id&post_type=attachment&post_mime_type=image&orderby={$orderby}"); | |
if ( empty($attachments) ) | |
return ''; | |
if ( is_feed() ) { | |
$output = "\n"; | |
foreach ( $attachments as $id => $attachment ) | |
$output .= wp_get_attachment_link($id, $size, true) . "\n"; | |
return $output; | |
} | |
// Build and markup it | |
$output = apply_filters('gallery_style', '<div id="gallery-' . $pid . '">'); | |
$output .= ('<!-- Begin -->'); | |
// Loop through each image | |
foreach ( $attachments as $id => $attachment ) { | |
// Attachment page ID | |
$att_page = get_attachment_link($id); | |
// Returns array | |
$img = wp_get_attachment_image_src($id, $image_size); | |
$img = $img[0]; | |
$thumb = wp_get_attachment_image_src($id, 'thumbnail'); | |
$thumb = $thumb[0]; | |
// Set the image titles | |
$title = $attachment->post_title; | |
// Get the Permalink | |
$permalink = get_permalink(); | |
// Set the image captions | |
$description = htmlspecialchars($attachment->post_content, ENT_QUOTES); | |
if($description == '') $description = htmlspecialchars($attachment->post_excerpt, ENT_QUOTES); | |
// Build html for each image | |
$output .= '<div class="thumbnail">'; | |
$output .= "<a href='".$img."'>"; | |
$output .= "<img src='".$thumb."' longdesc='".$permalink."' alt='".$description."' title='".$description."' />"; | |
$output .= "</a>"; | |
/* | |
if you planing to add title and descriptions | |
$output .= "<strong>".$title."</strong>"; | |
$output .= "<span>".$description."</span>"; | |
*/ | |
$output .= "</div>"; | |
// End foreach | |
} | |
// Close markup | |
$output .= "</div>"; | |
$output .= "<!-- Ends :) -->"; | |
return $output; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment