Forum Replies Created

Viewing 1 replies (of 1 total)
  • Forum: Plugins
    In reply to: WP2.5 Gallery pagination?
    smooth-criminal

    (@smooth-criminal)

    well i wrote some very Very VERY DUMB code that does what it needs to split the gallery into pages. In worst case scenario (last page) it queries all thumbnail but still displays only limited amount.

    this script registers new shortcode [mygallery] which accepts new parameter limit=x and it’s compatible with built in wp gallery so other options and cutomisations still apply.

    example: [mygallery limit=”20″]

    it puts some ugly $_GET values in url but still: does it’s thing

    da CODE (paste it in functions.php):

    <?php
    add_shortcode('mygallery', 'mygallery_shortcode');
    
    /**
     * The myGallery shortcode.
     *
     * This implements the functionality of the Gallery Shortcode for displaying
     * WordPress images on a post.
     *
     * @since 2.5.0
     *
     * @param array $attr Attributes attributed to the shortcode.
     * @return string HTML content to display gallery.
     */
    function mygallery_shortcode($attr) {
    	global $post;
    
    	// Allow plugins/themes to override the default gallery template.
    	$output = apply_filters('post_gallery', '', $attr);
    	if ( $output != '' )
    		return $output;
    
    	// 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'] );
    	}
    
    	if (!isset($_GET['gal_page'])) $gal_page = 0; else $gal_page = $_GET['gal_page'];
    	extract(shortcode_atts(array(
    		'order'      => 'ASC',
    		'orderby'    => 'menu_order ID',
    		'id'         => $post->ID,
    		'itemtag'    => 'dl',
    		'icontag'    => 'dt',
    		'captiontag' => 'dd',
    		'columns'    => 3,
    		'size'       => 'thumbnail',
    		'limit'      => 0
    	), $attr));
    
    	$id = intval($id);
    	$attachments = get_children( array('post_parent' => $id, 'post_status' => 'inherit', 'post_type' => 'attachment', 'post_mime_type' => 'image', 'order' => $order, 'orderby' => $orderby, 'numberposts' => $limit*(1+$gal_page)) );
    
    	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;
    	}
    
    	$itemtag = tag_escape($itemtag);
    	$captiontag = tag_escape($captiontag);
    	$columns = intval($columns);
    	$itemwidth = $columns > 0 ? floor(100/$columns) : 100;
    
    	if($gal_page > 0)
    	$previous_link = '<a href="'.get_permalink() .'&gal_page='. ($gal_page-1) .'#gallery-'.$id.'">Previous Page</a>';
    	if(((count($attachments)) - ($gal_page+1)*$limit) == 0)
    	$next_link = '<a href="'.get_permalink() .'&gal_page='. ($gal_page+1) .'#gallery-'.$id.'">Next Page</a>';
    
    	$output = apply_filters('gallery_style', "
    		<style type='text/css'>
    			.gallery {
    				margin: auto;
    			}
    			.gallery-item {
    				float: left;
    				margin-top: 10px;
    				text-align: center;
    				width: {$itemwidth}%;			}
    			.gallery img {
    				border: 2px solid #cfcfcf;
    			}
    			.gallery-caption {
    				margin-left: 0;
    			}
    		</style>
    		<!-- see mygallery_shortcode() in themes/totalwar/functions.php -->
    		<div id='gallery-".$id."' class='gallery'>".$next_link.$previous_link.'<br style="clear: both" />');
    
    	$i = 0;
    	$j = 0;
    	$skip = $limit*$gal_page;
    	foreach ( $attachments as $id => $attachment ) {
    		if($j++ < $skip) continue;
    		$link = isset($attr['link']) && 'file' == $attr['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='gallery-caption'>
    				{$attachment->post_excerpt}
    				</{$captiontag}>";
    		}
    		$output .= "</{$itemtag}>";
    		if ( $columns > 0 && ++$i % $columns == 0 )
    			$output .= '<br style="clear: both" />';
    	}
    
    	$output .= "
    			<br style='clear: both;' />".$next_link.$previous_link.'<br style="clear: both" />'."
    		</div>\n";
    
    	return $output;
    }

Viewing 1 replies (of 1 total)