Created
August 16, 2012 00:15
-
-
Save claudiosanches/3364947 to your computer and use it in GitHub Desktop.
Galeria com Páginação
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 | |
/* | |
Plugin Name: Galeria com paginação | |
Plugin URI: http://www.phil-barker.com/ | |
Description: Adds pages to the wordpress gallery | |
Version: 0.2 | |
Author: Claudio Sanches | |
Author URI: http://www.claudiosmweb.com/ | |
License: GPL2 | |
Plugin originalmente escrito por Phil Barker (http://www.phil-barker.com/) | |
*/ | |
function gallery_with_pagination($attr) { | |
global $post; | |
static $instance = 0; | |
$instance++; | |
extract( shortcode_atts( array( | |
'qty' => '5' | |
), $attr ) ); | |
$imagesPerPage = $qty; | |
// Define some default options | |
$options = array( | |
'order'=> 'ASC', | |
'orderby'=> 'menu_order ID', | |
'itemtag'=> 'dl', | |
'icontag'=> 'dt', | |
'captiontag'=> 'dd', | |
'columns'=> 3, | |
'size'=> 'thumbnail', | |
'perpage'=> $imagesPerPage, | |
'link'=>'attachment', | |
'show_edit_links'=>'Y', | |
'use_shortcode'=>'gallery', | |
'exclude'=>'' | |
); | |
// 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'] ); | |
} | |
// Overwrite the defaults with any options passed in | |
if (is_array($attr)) $options = array_merge($options, $attr); | |
// Start by getting the attachments | |
$attachments = get_children(array( | |
'post_parent'=> $post->ID, | |
'post_status'=>'inherit', | |
'post_type'=> 'attachment', | |
'post_mime_type'=>'image', | |
'order'=> $options['order'], | |
'orderby'=> $options['orderby'], | |
'exclude'=> $options['exclude'] | |
)); | |
// If we don't have any attachments - output nothing | |
if ( empty($attachments) ) return ''; | |
// Output feed if requested | |
if ( is_feed() ) { | |
$output = "\n"; | |
foreach ( $attachments as $id => $attachment ) | |
$output .= wp_get_attachment_link($id, $options['size'], true) . "\n"; | |
return $output; | |
} | |
// Standard post output | |
// Work out how many pages we need and what page we are currently on | |
$imageCount = count($attachments); | |
$pageCount = ceil($imageCount / $imagesPerPage); | |
$currentPage = intval($_GET['galleryPage']); | |
if ( empty($currentPage) || $currentPage<=0 ) $currentPage=1; | |
$maxImage = $currentPage * $imagesPerPage; | |
$minImage = ($currentPage-1) * $imagesPerPage; | |
if ($pageCount > 1) | |
{ | |
$page_link= get_permalink(); | |
$page_link_perma= true; | |
if ( strpos($page_link, '?')!==false ) | |
$page_link_perma= false; | |
$gplist= '<div class="gallery_pages_list">'.__('Pages').' '; | |
for ( $j=1; $j<= $pageCount; $j++) | |
{ | |
if ( $j==$currentPage ) | |
$gplist .= '<strong class="current_gallery_page_num"> '.$j.' </strong> '; | |
else | |
$gplist .= '<a href="'.$page_link. ( ($page_link_perma?'?':'&') ). 'galleryPage='.$j.'">'.$j.'</a> '; | |
} | |
$gplist .= '</div>'; | |
} | |
else | |
$gplist= ''; | |
$itemtag = tag_escape($options['itemtag']); | |
$captiontag = tag_escape($options['captiontag']); | |
$columns = intval($options['columns']); | |
$itemwidth = $options['columns'] > 0 ? floor(100/$options['columns']) : 100; | |
$float = is_rtl() ? 'right' : 'left'; | |
$icontag = $options['icontag']; | |
$id = isset($options['id']) ? $options['id'] : ''; | |
$size = $options['size']; | |
$selector = "gallery-{$instance}"; | |
$gallery_style = $gallery_div = ''; | |
if ( apply_filters( 'use_default_gallery_style', true ) ) | |
$gallery_style = " | |
<style type='text/css'> | |
#{$selector} { | |
margin: auto; | |
} | |
#{$selector} .gallery-item { | |
float: {$float}; | |
margin-top: 10px; | |
text-align: center; | |
width: {$itemwidth}%; | |
} | |
#{$selector} img { | |
border: 2px solid #cfcfcf; | |
} | |
#{$selector} .gallery-caption { | |
margin-left: 0; | |
} | |
</style> | |
<!-- see gallery_shortcode() in wp-includes/media.php -->"; | |
$size_class = sanitize_html_class( $size ); | |
$gallery_div = "<div id='$selector' class='gallery galleryid-{$id} gallery-columns-{$columns} gallery-size-{$size_class}'>"; | |
$output = apply_filters( 'gallery_style', $gallery_style . "\n\t\t" . $gallery_div ); | |
$i = 0; | |
$k = 0; | |
foreach ( $attachments as $id => $attachment ) { | |
if ($k >= $minImage && $k < $maxImage) { | |
$link = isset($options['link']) && 'file' == $options['link'] ? wp_get_attachment_link($id, $size, false, false) : wp_get_attachment_link($id, $size, true, false); | |
$output .= "<{$itemtag} class='gallery-item'>"; | |
$output .= " | |
<{$icontag} class='gallery-icon'> | |
$link | |
</{$icontag}>"; | |
if ( $captiontag && trim($attachment->post_excerpt) ) { | |
$output .= " | |
<{$captiontag} class='wp-caption-text gallery-caption'> | |
" . wptexturize($attachment->post_excerpt) . " | |
</{$captiontag}>"; | |
} | |
$output .= "</{$itemtag}>"; | |
if ( $columns > 0 && ++$i % $columns == 0 ) | |
$output .= '<br style="clear: both" />'; | |
} | |
$k++; | |
} | |
$output .= "\n<br style='clear: both;' />$gplist\n</div>\n"; | |
return $output; | |
// If we've got this far then we must have some attachments to play with! | |
//return 'Gallery Here - Page '. $currentPage. ' de '. $pageCount; | |
} | |
add_shortcode('page-gallery', 'gallery_with_pagination'); | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment