<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>The Code Dojo &#187; gallery</title>
	<atom:link href="http://dojo.codegreene.com/tag/gallery/feed/" rel="self" type="application/rss+xml" />
	<link>http://dojo.codegreene.com</link>
	<description>The Code Dojo is the veritable repository of random musings from the development team at Code Greene.</description>
	<lastBuildDate>Fri, 20 Jan 2012 18:09:02 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Filter images from WordPress &#x5b;gallery]</title>
		<link>http://dojo.codegreene.com/2009/05/filter-images-from-wordpress-gallery/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=filter-images-from-wordpress-gallery</link>
		<comments>http://dojo.codegreene.com/2009/05/filter-images-from-wordpress-gallery/#comments</comments>
		<pubDate>Thu, 07 May 2009 15:46:40 +0000</pubDate>
		<dc:creator>Benjam</dc:creator>
				<category><![CDATA[Usability]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[gallery]]></category>

		<guid isPermaLink="false">http://dojo.codegreene.com/?p=259</guid>
		<description><![CDATA[EDIT: This post has been deprecated by the recently released WordPress 2.9. Please update your WordPress installation to get this functionality in your blog. If you don&#8217;t wish to update your blog software, then please continue reading. I was writing a post on my personal blog recently and wanted to show a gallery of pictures, [...]]]></description>
			<content:encoded><![CDATA[<p><strong>EDIT</strong>: This post has been deprecated by the recently released WordPress 2.9.  Please <a href="http://wordpress.org/download/">update your WordPress installation</a> to get this functionality in your blog.</p>
<p>If you don&#8217;t wish to update your blog software, then please continue reading.</p>
<hr />
<p>I was writing a post on my personal blog recently and wanted to show a gallery of pictures, but I also wanted to show one of those pictures near the top of the post, but not in the gallery, and if you&#8217;ve ever tried to do this with <tt>&#x5b;gallery]</tt>, you know it can&#8217;t be done.</p>
<p>Well, now it can, and I&#8217;ll show you how.</p>
<p><span id="more-259"></span>First we take a look at the original WP function that creates the gallery.  The function is called <tt>gallery_shortcode( )</tt> and it&#8217;s located in the <tt>/wp-includes/media.php</tt> file.  Make a copy of this function and paste it in your <tt>/wp-content/themes/[your_theme]/functions.php</tt> file, or create a plugin and paste it there.</p>
<p>Now that that&#8217;s pasted, lets take a look at it.</p>
<p>Near the top of the function, you&#8217;ll find the hook we&#8217;ll be using to modify the gallery function and output.  It looks like the following:</p>
<pre class="brush: php">
	// Allow plugins/themes to override the default gallery template.
	$output = apply_filters(&#039;post_gallery&#039;, &#039;&#039;, $attr);
	if ( $output != &#039;&#039; )
		return $output;
</pre>
<p>So the first thing we need to do is remove that portion from our function, because it doesn&#8217;t make any sense to hook a callback to itself.</p>
<p>The next thing we need to do is edit the function attributes to match the attributes in the hook. Here is the new function definition:</p>
<pre class="brush: php">
function my_gallery_shortcode($null, $attr = array( )) {
</pre>
<p>The <var>$null</var> attribute takes the empty string that gets passed and then just ignores it, we don&#8217;t need it here.  The <var>$args</var> attribute takes the <var>$attr</var> variable that gets passed.  you&#8217;ll also note that we renamed the function so we don&#8217;t get naming conflicts.  Just under the function, right after the closing brace ( } ), add the following:</p>
<pre class="brush: php">
add_filter(&#039;post_gallery&#039;, &#039;my_gallery_shortcode&#039;, 10, 2);
</pre>
<p>This hooks the function we&#8217;re creating into the original function and overrides it.</p>
<p>Now that we have that, we take a look at where the function is pulling it&#8217;s data from, so we can edit that data and filter the output to exclude certain images from the gallery.  Just under where the shortcode attributes are extracted, you&#8217;ll see a call to <tt>get_children( )</tt>.  This is the function that pulls the gallery image data from the database.  After looking through the source, I noticed there was an <var>exclude</var> attribute that was available to this function, but it wasn&#8217;t being used here.  This will be the key to fixing this function.</p>
<p>Add the <var>exclude</var> attribute to the <tt>get_children( )</tt> function call, and add it to the shortcode attribute extract portion of our function as well, so we get what we need out of the <var></var> shortcode and don&#8217;t discard it along the way.  Here is the new portion of the function, with a little bit of code before and after for orientation:</p>
<pre class="brush: php">
	// We&#039;re trusting author input, so let&#039;s at least make sure it looks like a valid orderby statement
	if ( isset( $attr[&#039;orderby&#039;] ) ) {
		$attr[&#039;orderby&#039;] = sanitize_sql_orderby( $attr[&#039;orderby&#039;] );
		if ( !$attr[&#039;orderby&#039;] )
			unset( $attr[&#039;orderby&#039;] );
	}

	extract(shortcode_atts(array(
		&#039;order&#039;      =&gt; &#039;ASC&#039;,
		&#039;orderby&#039;    =&gt; &#039;menu_order ID&#039;,
		&#039;id&#039;         =&gt; $post-&gt;ID,
		&#039;itemtag&#039;    =&gt; &#039;dl&#039;,
		&#039;icontag&#039;    =&gt; &#039;dt&#039;,
		&#039;captiontag&#039; =&gt; &#039;dd&#039;,
		&#039;columns&#039;    =&gt; 3,
		&#039;size&#039;       =&gt; &#039;thumbnail&#039;,
		&#039;exclude&#039;    =&gt; &#039;&#039; //  $id, &#039;post_status&#039; =&gt; &#039;inherit&#039;,
		&#039;post_type&#039; =&gt; &#039;attachment&#039;, &#039;post_mime_type&#039; =&gt; &#039;image&#039;, &#039;order&#039; =&gt; $order,
		&#039;orderby&#039; =&gt; $orderby, &#039;exclude&#039; =&gt; $exclude) );
		// the &#039;exclude&#039; portion at the end was added

	if ( empty($attachments) )
		return &#039;&#039;;
</pre>
<p>Now we have a function that pulls only the gallery images we want, excluding the ones we don&#8217;t want.  To use, switch your editor to HTML mode and insert the image you wish to exclude from the gallery (I apologize, but there is no other quick way that I know of to get the post id of the image besides inserting it into the post and reading from there, but I want to exclude images from the gallery because I&#8217;ve used them elsewhere in the post, so it wasn&#8217;t a big deal for me), in the image tag class attribute, you&#8217;ll see something like &#8220;wp-image-1822&#8243;, where the number is different for every image, this is the post id and that&#8217;s what you need to insert in your gallery shortcode to exclude as follows (remove the leading dot):</p>
<pre class="brush: text">
[.gallery exclude=&quot;1822,1845,1236&quot;]
</pre>
<p>And there you have it, images excluded from your gallery.</p>
<p>Here is my complete function and hook.  Note I also made some CSS changes to fit the gallery in with my floated right sidebar, which was breaking due to the floated gallery images and the clears that were on them.  You can use this as is, or edit to suit your needs.</p>
<pre class="brush: php">
/**
 * The Gallery shortcode.
 *
 * This alters the functionality of the Gallery Shortcode for displaying
 * WordPress images on a post.
 * This function also adds an aditional option to the gallery shortcode:
 *   exclude = excludes the list of post (image) ids from the gallery
 *
 * @since 2.5.0
 *
 * @param mixed $null space filler to take up empty string argument passed by WP
 * @param array $attr Attributes attributed to the shortcode.
 * @return string HTML content to display gallery.
 */
function my_gallery_shortcode($null, $attr = array( )) {
	global $post;

	// We&#039;re trusting author input, so let&#039;s at least make sure it looks like a valid orderby statement
	if ( isset( $attr[&#039;orderby&#039;] ) ) {
		$attr[&#039;orderby&#039;] = sanitize_sql_orderby( $attr[&#039;orderby&#039;] );
		if ( !$attr[&#039;orderby&#039;] )
			unset( $attr[&#039;orderby&#039;] );
	}

	extract(shortcode_atts(array(
		&#039;order&#039;      =&amp;gt; &#039;ASC&#039;,
		&#039;orderby&#039;    =&amp;gt; &#039;menu_order ID&#039;,
		&#039;id&#039;         =&amp;gt; $post-&amp;gt;ID,
		&#039;itemtag&#039;    =&amp;gt; &#039;dl&#039;,
		&#039;icontag&#039;    =&amp;gt; &#039;dt&#039;,
		&#039;captiontag&#039; =&amp;gt; &#039;dd&#039;,
		&#039;columns&#039;    =&amp;gt; 3,
		&#039;size&#039;       =&amp;gt; &#039;thumbnail&#039;,
		&#039;exclude&#039;    =&amp;gt; &#039;&#039;
	), $attr));

	$id = intval($id);
	$attachments = get_children( array(&#039;post_parent&#039; =&amp;gt; $id, &#039;post_status&#039; =&amp;gt; &#039;inherit&#039;,
	&#039;post_type&#039; =&amp;gt; &#039;attachment&#039;, &#039;post_mime_type&#039; =&amp;gt; &#039;image&#039;, &#039;order&#039; =&amp;gt; $order,
	&#039;orderby&#039; =&amp;gt; $orderby, &#039;exclude&#039; =&amp;gt; $exclude) );

	if ( empty($attachments) )
		return &#039;&#039;;

	if ( is_feed( ) ) {
		$output = &quot;\n&quot;;
		foreach ( $attachments as $id =&amp;gt; $attachment )
			$output .= wp_get_attachment_link($id, $size, true) . &quot;\n&quot;;
		return $output;
	}

	$itemtag = tag_escape($itemtag);
	$captiontag = tag_escape($captiontag);
	$columns = intval($columns);
	$itemwidth = $columns &amp;gt; 0 ? floor(100/$columns) : 100;

	$output = apply_filters(&#039;gallery_style&#039;, &quot;

	.gallery {
		margin: auto;
	}
	.gallery:after {
		content: &#039;.&#039;;
		display: block;
		height: 0;
		clear: left;
		visibility: hidden;
	}
	.gallery-item {
		float: left;
		margin-top: 10px;
		text-align: center;
		width: {$itemwidth}%;
	}
	.gallery-caption {
		margin-left: 0;
	}

	&lt;!-- see my_gallery_shortcode( ) in {theme_dir}/functions.php --&gt;
	&lt;div class=&#039;gallery&#039;&gt;&quot;);

	$i = 0;
	foreach ( $attachments as $id =&amp;gt; $attachment ) {
		$link = isset($attr[&#039;link&#039;]) &amp;amp;&amp;amp; &#039;file&#039; == $attr[&#039;link&#039;]
			? wp_get_attachment_link($id, $size, false, false)
			: wp_get_attachment_link($id, $size, true, false);

		$output .= &quot;&quot;;
		$output .= &quot;

		$link
		&quot;;
		if ( $captiontag &amp;amp;&amp;amp; trim($attachment-&amp;gt;post_excerpt) ) {
			$output .= &quot;

			{$attachment-&amp;gt;post_excerpt}
			&quot;;
		}
		$output .= &quot;&quot;;
	}

	$output .= &quot;
	&lt;/div&gt;\n&quot;;

	return $output;
}
add_filter(&#039;post_gallery&#039;, &#039;my_gallery_shortcode&#039;, 10, 2);
</pre>
<p>I hope this helps somebody, and please leave any questions or comments below.</p>
]]></content:encoded>
			<wfw:commentRss>http://dojo.codegreene.com/2009/05/filter-images-from-wordpress-gallery/feed/</wfw:commentRss>
		<slash:comments>23</slash:comments>
		</item>
	</channel>
</rss>

