<?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>Digging into WordPress &#187; lightbox</title>
	<atom:link href="http://digwp.com/tag/lightbox/feed/" rel="self" type="application/rss+xml" />
	<link>http://digwp.com</link>
	<description>Take your WordPress skills to the next level.</description>
	<lastBuildDate>Fri, 18 May 2012 18:21:22 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>Create a Stunning Lightbox-Style Random-Post Header Gallery</title>
		<link>http://digwp.com/2009/06/random-lightbox-header-gallery/</link>
		<comments>http://digwp.com/2009/06/random-lightbox-header-gallery/#comments</comments>
		<pubDate>Mon, 22 Jun 2009 14:09:43 +0000</pubDate>
		<dc:creator>Jeff Starr</dc:creator>
				<category><![CDATA[Design]]></category>
		<category><![CDATA[custom-fields]]></category>
		<category><![CDATA[header]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[layout]]></category>
		<category><![CDATA[lightbox]]></category>
		<category><![CDATA[loop]]></category>

		<guid isPermaLink="false">http://diggingintowordpress.com/?p=150</guid>
		<description><![CDATA[In this tutorial, we&#8217;re going to take advantage of two of WordPress&#8217; most powerful features, get_posts() and custom fields, to create a stunning random lightbox-style header gallery for your post images. Displayed before the standard post loop, this lightbox gallery will randomly display the images that are associated with your posts while also providing a [...]]]></description>
			<content:encoded><![CDATA[<p>In this tutorial, we&rsquo;re going to take advantage of two of WordPress&rsquo; most powerful features, <code>get_posts()</code> and custom fields, to create a stunning random lightbox-style header gallery for your post images. Displayed <em>before</em> the standard post loop, this lightbox gallery will randomly display the images that are associated with your posts while also providing a descriptive title link to the post itself. Here is a graphical representation that will help us visualize what we are going to do:</p>
<p><img src="http://digwp.com/wp-content/blog-images/lightbox-tutorial.jpg" alt="" title="" /><br /><small>Random post-display functionality with lightbox-style popup image and title post link</small></p>
<p>The key to setting this up involves creating a set of custom fields for each post and then displaying the information randomly before the main post loop. Let&rsquo;s begin with the custom fields.</p>
<p><span id="more-150"></span></p>
<h3>Setting up the custom fields</h3>
<p>If you have already been taking advantage of <a href="http://perishablepress.com/press/2008/12/17/wordpress-custom-fields-tutorial/" title="WordPress Custom Fields, Part I: The Basics">WordPress custom fields</a>, then you already understand the basic idea here. For each post that you would like to have appear randomly in the header of your theme, create the following custom fields:</p>
<p><img src="http://digwp.com/wp-content/blog-images/custom-fields.gif" alt="" title="" /><br /><small>Custom-field data associated with each randomly displayed post</small></p>
<p>Notice that we are using <em>relative</em> file paths for the images. This trick ensures maximum flexibility and portability for our custom-field data. Also note the two different images we are using for each post: a full-size image that will be displayed in the lightbox popup, and a cropped or resized version of that same image for display in the random header.</p>
<p>Once you have associated these custom fields with a few of your posts, it is time for a little <code>get_posts()</code> action to display the random content in your active theme.</p>
<h3>Editing your WordPress theme files</h3>
<p>One of the great things about WordPress custom fields is that they enable you to display your content in just about any way imaginable. In this tutorial, we are going to display our custom-field data in the header area for every page on the site. Thus, we open our theme&rsquo;s <code>header.php</code> file and add the following code:</p>
<pre><code>&lt;?php $random = get_posts('showposts=1&amp;orderby=rand'); foreach($random as $post) : setup_postdata($post); ?&gt;

&lt;p&gt;
	&lt;a href="http://domain.tld&lt;?php echo get_post_meta($post-&gt;ID, 'randomImage', true); ?&gt;" title="&lt;?php echo get_post_meta($post-&gt;ID, 'randomCaption', true); ?&gt;" class="thickbox"&gt;
		&lt;img src="http://domain.tld&lt;?php echo get_post_meta($post-&gt;ID, 'randomBanner', true); ?&gt;" alt="" /&gt;
	&lt;/a&gt;
	&lt;small&gt;&lt;a href="http://domain.tld&lt;?php echo get_post_meta($post-&gt;ID, 'randomLink', true); ?&gt;"&gt;&lt;?php echo get_post_meta($post-&gt;ID, 'randomTitle', true); ?&gt;&lt;/a&gt;&lt;/small&gt;
&lt;/p&gt;

&lt;?php endforeach; ?&gt;</code></pre>
<p>As mentioned, this code goes before the normal WordPress loop and calls data from each of the five custom fields that have been added to your posts. Instead of using the more powerful <code>query_posts()</code> for this random post display, we use the simpler <a href="http://codex.wordpress.org/Template_Tags/get_posts" title="WordPress Codex: Template Tags/get posts">get_posts()</a> function instead. It does everything we need while eliminating unnecessary variables and potential complications.</p>
<p>A few other points of interest for this WordPress snippet; notice we are setting the randomness and limiting the post display to one (&nbsp;<code>1</code>&nbsp;) via two parameters in the <code>get_posts()</code> function. Once the query has been setup, we are using the <a href="http://codex.wordpress.org/Function_Reference/get_post_meta" title="Function Reference/get post meta">get_post_meta()</a> tag to populate the following markup with their respective custom-field values:</p>
<pre><code>&lt;p&gt;
	&lt;a href="[randomImage]" title="[randomCaption]" class="thickbox"&gt;
		&lt;img src="[randomBanner]" alt="" /&gt;
	&lt;/a&gt;
	&lt;small&gt;&lt;a href="[randomLink]"&gt;[randomTitle]&lt;/a&gt;&lt;/small&gt;
&lt;p&gt;</code></pre>
<p>This demonstrates the incredible design flexibility enabled by WordPress custom fields. Once this code is in place, it is time to wrap things up by implementing the JavaScript-powered lightbox functionality.</p>
<h3>Adding the JavaScript</h3>
<p>To finish things off, let&rsquo;s make that random-banner image really &ldquo;pop&rdquo; by implementing some JavaScript-powered lightbox functionality. As you probably know, there are <a href="http://planetozh.com/projects/lightbox-clones/" title="The Lightbox Clones Matrix">many lightbox clones</a> from which to choose, but for this tutorial we turn to Cody Lindley&rsquo;s excellent <a href="http://jquery.com/demo/thickbox/" title="ThickBox 3.1">Thickbox</a> script:</p>
<blockquote><p>ThickBox is a webpage UI dialog widget written in JavaScript on top of the jQuery library. Its function is to show a single image, multiple images, inline content, iframed content, or content served through AJAX in a hybrid modal.</p></blockquote>
<p>Using Thickbox is <em>so</em> easy, it almost feels like I&rsquo;m just being lazy for using it. In fact, we have already setup our WordPress code for use with Thickbox by simply adding the <code>class="thickbox"</code> attribute to the random-banner anchor element. The only other thing that really needs to be done is to upload the Thickbox script and accompanying <acronym title="Cascading Style Sheets">CSS</acronym> file to your server and link to them from the <code>&lt;head&gt;</code> section of your <code>header.php</code> file:</p>
<pre><code>&lt;link rel="stylesheet" href="http://domain.tld/wp-content/css/thickbox.css" type="text/css" media="screen" /&gt;
&lt;script type="text/javascript" src="http://domain.tld/wp-content/javascript/thickbox-compressed.js"&gt;&lt;/script&gt;</code></pre>
<h3>Putting it all together</h3>
<p>Once everything is in place, your random-post gallery should be working great. To see a working example of this technique, check out the header area of <a href="http://eightyeightteeth.com/" title="EightyEightTeeth.com - Graphics Portfolio of Thane Champie">Thane Champie&rsquo;s Graphics Portfolio site</a>. That particular implementation of this method features a few more bells and whistles, but the basic functionality is the same.</p>
<hr />
<p><small>© 2009 <a href="http://digwp.com">Digging into WordPress</a> | <a href="http://digwp.com/2009/06/random-lightbox-header-gallery/">Permalink</a> | <a href="http://digwp.com/2009/06/random-lightbox-header-gallery/#comments">15 comments</a> | Add to <a href="http://del.icio.us/post?url=http://digwp.com/2009/06/random-lightbox-header-gallery/&title=Create a Stunning Lightbox-Style Random-Post Header Gallery">del.icio.us</a> | Post tags: <a href="http://digwp.com/tag/custom-fields/" rel="tag">custom-fields</a>, <a href="http://digwp.com/tag/header/" rel="tag">header</a>, <a href="http://digwp.com/tag/javascript/" rel="tag">JavaScript</a>, <a href="http://digwp.com/tag/layout/" rel="tag">layout</a>, <a href="http://digwp.com/tag/lightbox/" rel="tag">lightbox</a>, <a href="http://digwp.com/tag/loop/" rel="tag">loop</a><br/></small></p>]]></content:encoded>
			<wfw:commentRss>http://digwp.com/2009/06/random-lightbox-header-gallery/feed/</wfw:commentRss>
		<slash:comments>15</slash:comments>
		</item>
	</channel>
</rss>

