All pastes #970789 Raw Edit

Mine

public text v1 · immutable
·published 2008-04-04 08:49 UTC
rendered paste body
<?php
/*
Plugin Name: mf Gallery Custom Short Code
Plugin URI: n/a
Description: Allows user to set a comma seperated string of ID's for the [GALLERY] Short tag.
Author: Michael Fields
Version: 0
Author URI: http://mfields.org/
*/
remove_shortcode('gallery_shortcode');

add_shortcode('gallery', 'mf_gallery_shortcode');

function mf_gallery_shortcode( $attr ) {
	global $post, $wpdb;

	// Allow plugins/themes to override the default gallery template.
	$output = apply_filters('post_gallery', '', $attr);
	if ( $output != '' )
		return $output;
		
	extract(shortcode_atts(array(
		'orderby'    => 'menu_order ASC, ID ASC',
		'ids'         => '',
		'id'         => $post->ID,
		'itemtag'    => 'dl',
		'icontag'    => 'dt',
		'captiontag' => 'dd',
		'columns'    => 3,
		'size'       => 'thumbnail',
	), $attr));

	$id = intval($id);
	$orderby = addslashes($orderby);
	
	if ( !empty( $ids ) ){
		$_ids = explode( ',', $ids );
		
		if ( isset( $_ids ) && is_array( $_ids ) ){
			foreach( $_ids as $k => $v ){
				$v = intval( trim( $v ) );
			}
			$clean->ids = implode( ',', $_ids );
		}

		$_attachments = $wpdb->get_results("
			SELECT *
			FROM $wpdb->posts
			WHERE `ID` IN(  $clean->ids )
			ORDER BY $orderby
			");
	}else{
		$_attachments = get_children("post_parent=$id&post_type=attachment&post_mime_type=image&orderby=\"{$orderby}\"");
	}
	
	$attachments = array();
	foreach( $_attachments as $order => $a ){
		$attachments[ $a->ID ] = $a;
	}
		
	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;
	}

	$listtag = tag_escape($listtag);
	$itemtag = tag_escape($itemtag);
	$captiontag = tag_escape($captiontag);
	$columns = intval($columns);
	$itemwidth = $columns > 0 ? floor(100/$columns) : 100;
	
	$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 gallery_shortcode() in wp-includes/media.php -->
		<div class='gallery'>");

	foreach ( $attachments as $id => $attachment ) {
		$link = wp_get_attachment_link($id, $size, true);
		$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;' >
		</div>\n";

	return $output;
}


?>