<?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>Jeff Gran &#187; WordPress</title>
	<atom:link href="http://jeffgran.com/tag/wordpress/feed" rel="self" type="application/rss+xml" />
	<link>http://jeffgran.com</link>
	<description>Artist, Designer, Developer</description>
	<lastBuildDate>Wed, 09 Nov 2011 15:59:31 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>WordPress: Sort Posts by Multiple Fields (Part II)</title>
		<link>http://jeffgran.com/262/blog/wordpress-sort-posts-by-multiple-fields-part-ii</link>
		<comments>http://jeffgran.com/262/blog/wordpress-sort-posts-by-multiple-fields-part-ii#comments</comments>
		<pubDate>Sun, 17 Jul 2011 23:17:39 +0000</pubDate>
		<dc:creator>Jeff Gran</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://jeffgran.com/?p=262</guid>
		<description><![CDATA[Here's a fast and easy way to sort your posts by multiple fields, using the posts_orderby filter hook and a simple function.]]></description>
			<content:encoded><![CDATA[<h3>The Sequel!</h3>
<p>Because my previous post (<a href="http://jeffgran.com/218/blog/wordpress-sort-posts-by-multiple-fields" title="WordPress: Sort Posts by Multiple Fields">WordPress: Sort Posts by Multiple Fields</a>) was one of my most popular posts, and because I have realized since then that the solution it proposes is an inadequate one, I felt the need to write an update.  I don&#8217;t want to be spreading bad information, and for a blog with any significant number of posts, that solution is untenable.</p>
<p>As <a href="http://jeffgran.com/218/blog/wordpress-sort-posts-by-multiple-fields#comment-254" title="WordPress: Sort Posts by Multiple Fields">progzy</a> points out, sorting by multiple fields can be accomplished using the `posts_orderby` filter hook, which uses MySQL directly to sort the posts, instead of getting all the posts from the database into a gigantic array, and then sorting them with php code. Here&#8217;s how to do it.</p>
<h3>Writing an orderby Function</h3>
<p>You&#8217;ll need to write a function that returns a SQL fragment represented as a string, and then connect it to the filter hook. The SQL fragment is the `ORDER BY` segment of the SQL query that gets your posts from the database. See <a href="http://dev.mysql.com/doc/refman/5.0/en/sorting-rows.html">the MySQL Documentation on sorting</a> for more info on this.</p>
<p>Here&#8217;s an example of a custom sorting function:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="color: #ccc;"><span style="color: #ffff00;">function</span> order_by_multiple<span style="color: grey;">&#40;</span> <span style="color: #ffc080;">$orderby</span><span style="color: grey;">&#41;</span> <span style="color: grey;">&#123;</span>
  <span style="color: #4080ff;">return</span> <span style="color: #ff8080;">&quot;post_date ASC, post_title DESC&quot;</span><span style="color: grey;">;</span>
<span style="color: grey;">&#125;</span></pre></div></div>

<p>The above would sort the posts by date in ascending order first, and secondarily by title in descending (reverse) order. </p>
<h3>Only Sort on a certain page</h3>
<p>You can also make this much more complex, if you want to. One important way is by using the different &#8220;<a href="http://codex.wordpress.org/Conditional_Tags">Conditional Tags</a>&#8221; WordPress provides. Here&#8217;s an example that only applies the custom sort on the home page:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="color: #ccc;"><span style="color: #ffff00;">function</span> order_by_multiple<span style="color: grey;">&#40;</span> <span style="color: #ffc080;">$orderby</span><span style="color: grey;">&#41;</span> <span style="color: grey;">&#123;</span>
  <span style="color: #4080ff;">if</span> <span style="color: grey;">&#40;</span><span style="color: grey;">!</span>is_home<span style="color: grey;">&#40;</span><span style="color: grey;">&#41;</span><span style="color: grey;">&#41;</span> <span style="color: grey;">&#123;</span>
    <span style="color: #4080ff;">return</span> <span style="color: #ff8080;">&quot;post_date ASC, post_title DESC&quot;</span><span style="color: grey;">;</span>
  <span style="color: grey;">&#125;</span>
<span style="color: grey;">&#125;</span></pre></div></div>

<h3>Hook up the sort function</h3>
<p>Then, once you have your ordering function written, you just need to hook it into the WordPress code like this:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="color: #ccc;">add_filter<span style="color: grey;">&#40;</span><span style="color: #ff8080;">&quot;posts_orderby&quot;</span><span style="color: grey;">,</span> <span style="color: #ff8080;">&quot;order_by_multiple&quot;</span><span style="color: grey;">&#41;</span><span style="color: grey;">;</span></pre></div></div>

<p>Make the second string argument there match your function name, of course (it doesn&#8217;t have to be the same name as mine, it can be anything). And you can have more than one function hook into the same hook. They will be executed in the same order they are added. In that case, the `$orderby` argument that&#8217;s coming into the function is the existing SQL fragment. So you could add to it, remove something from it, etc.</p>
<h3>Use Debug Query to help figure out what to return</h3>
<p>So the question is, how do you know what to put in your SQL fragment string? If you know your SQL well, you might be able to figure it out by intuition/trial-and-error. For the rest of us, there is a very helpful WordPress Plugin called <a href="http://wordpress.org/extend/plugins/debug-queries/">Debug Queries</a> which will print out all of the MySQL queries that are run for every page you visit (only for logged-in admins, of course. It&#8217;s just a debugging tool for use while developing).</p>
<p>If you install it, it will print out many queries per page. You have to look for the one that&#8217;s querying for posts. In my testing, mine looks like this:</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="color: #ccc;"><span style="color: #4080ff;">SELECT</span> SQL_CALC_FOUND_ROWS wp_posts<span style="color: grey;">.*</span> <span style="color: #4080ff;">FROM</span> wp_posts <span style="color: grey;">&#40;</span><span style="color: grey;">...</span><span style="color: grey;">&#41;</span></pre></div></div>

<p>The important part is the `FROM wp_posts`. That means you&#8217;re selecting rows from the posts table. So if you have plugins or metadata that you&#8217;re selecting on, you will see them in that query, and it may help you figure out what incantation you need to put in that `orderby` string.</p>
<p>If you need to sort by category, tag, custom meta-data, etc. as one of your sort values, you may need to alter your MySQL query even more, perhaps <a href="http://codex.wordpress.org/Custom_Queries">using other hooks</a> (very similar to the `posts_orderby` hook), such as `posts_join`, `posts_where`, etc. Each of these modify different parts of the query.</p>
<p>Beyond that, if you still need help figuring out how to get the exact set of posts in the right order, you may need to read up on MySQL in general and learn how to manipulate the queries in more detail. Good Luck!</p>
]]></content:encoded>
			<wfw:commentRss>http://jeffgran.com/262/blog/wordpress-sort-posts-by-multiple-fields-part-ii/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>WordPress: Sort Posts by Multiple Fields</title>
		<link>http://jeffgran.com/218/blog/wordpress-sort-posts-by-multiple-fields</link>
		<comments>http://jeffgran.com/218/blog/wordpress-sort-posts-by-multiple-fields#comments</comments>
		<pubDate>Tue, 21 Jul 2009 21:05:16 +0000</pubDate>
		<dc:creator>Jeff Gran</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://jeffgran.com/?p=218</guid>
		<description><![CDATA[UPDATE: I have written a new post about how to better accomplish this task: WordPress: Sort Posts by Multiple Fields (Part II) Please see the above link for a much more efficient way to sort by multiple fields. I was recently creating a star-rating system for a client, and we were sorting the reviews (or [...]]]></description>
			<content:encoded><![CDATA[<h3>UPDATE: I have written a new post about how to better accomplish this task: <a href="http://jeffgran.com/262/blog/wordpress-sort-posts-by-multiple-fields-part-ii">WordPress: Sort Posts by Multiple Fields (Part II)</a></h3>
<p>Please see the above link for a much more efficient way to sort by multiple fields.</p>
<p>I was recently creating a star-rating system for a client, and we were sorting the reviews (or &#8220;posts&#8221;) by the number of stars.  The problem that came up was that with a 5 star system, and increments of half-stars, you will necessarily end up with many of the ratings being equal.  We wanted to be able to control the exact order the reviews were shown in the case of a tie.  Here&#8217;s how I did it.</p>
<h3>Adding a &#8220;Priority&#8221; Custom Field</h3>
<p>The first step was to add a custom field called &#8220;priority&#8221;, which would act as the tie-breaker.  Basically, if two posts have the same star rating, we&#8217;ll check the priority and see which one is higher, and that one will be displayed first.  I was using the <a title="Flutter WordPress Plugin Homepage" href="http://flutter.freshout.us/" target="_blank">Flutter plugin</a>, so adding another custom field was as simple as clicking a few buttons in the admin panel.</p>
<h3>Custom Query</h3>
<p>Sorting by a single field is relatively easy.  You would create a custom query for the posts by using something like this (to sort alphabetically):</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="color: #ccc;">query_posts<span style="color: grey;">&#40;</span><span style="color: #ff8080;">&quot;orderby=title&amp;order=ASC&quot;</span><span style="color: grey;">&#41;</span><span style="color: grey;">;</span></pre></div></div>

<p>Using Flutter&#8217;s syntax for sorting by its custom fields, we&#8217;d use:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="color: #ccc;">query_posts<span style="color: grey;">&#40;</span><span style="color: #ff8080;">&quot;customorderby=x_stars-overall-rating&amp;order=DESC&quot;</span><span style="color: grey;">&#41;</span><span style="color: grey;">;</span></pre></div></div>

<h3>Refining the Order with usort()</h3>
<p>But you can&#8217;t sort by more than one field using the above methods.  That&#8217;s where usort comes in.  PHP has <a title="PHP Manual: usort" href="http://us2.php.net/manual/en/function.usort.php" target="_blank">a function called usort()</a> which sorts an array based on a custom function which &#8220;compares&#8221; the two array items in any way you like.  So all we need to do is run our array of posts through a function which does the more complex comparison using both the &#8220;stars&#8221; field and then the &#8220;priority&#8221; field for tie-breakers.</p>
<p>First, we insert this in the category listing page (or whichever page you&#8217;re working on) to run the custom sort function:</p>

<div class="wp_syntax"><div class="code"><pre class="php" style="color: #ccc;"><span style="color: cyan;">usort</span><span style="color: grey;">&#40;</span><span style="color: #ffc080;">$wp_query</span><span style="color: grey;">-&gt;</span><span style="color: magenta;">posts</span><span style="color: grey;">,</span> <span style="color: #0000ff;">'review_tie_breaker'</span><span style="color: grey;">&#41;</span><span style="color: grey;">;</span></pre></div></div>

<p>The first argument in the function is the array of posts created by the default query in WordPress.  The second argument is the name of our custom sort function, which is shown below:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
</pre></td><td class="code"><pre class="php" style="color: #ccc;"><span style="color: #ffff00;">function</span> review_tie_breaker<span style="color: grey;">&#40;</span><span style="color: #ffc080;">$a</span><span style="color: grey;">,</span> <span style="color: #ffc080;">$b</span><span style="color: grey;">&#41;</span> <span style="color: grey;">&#123;</span>
   <span style="color: #80ff80">// get the star value for each </span>
   <span style="color: #ffc080;">$a_stars</span> <span style="color: grey;">=</span> get_post_meta<span style="color: grey;">&#40;</span><span style="color: #ffc080;">$a</span><span style="color: grey;">-&gt;</span><span style="color: magenta;">ID</span><span style="color: grey;">,</span> <span style="color: #0000ff;">'stars-overall-rating'</span><span style="color: grey;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: grey;">&#41;</span><span style="color: grey;">;</span>
   <span style="color: #ffc080;">$b_stars</span> <span style="color: grey;">=</span> get_post_meta<span style="color: grey;">&#40;</span><span style="color: #ffc080;">$b</span><span style="color: grey;">-&gt;</span><span style="color: magenta;">ID</span><span style="color: grey;">,</span> <span style="color: #0000ff;">'stars-overall-rating'</span><span style="color: grey;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: grey;">&#41;</span><span style="color: grey;">;</span>
   <span style="color: #80ff80">// if it's not a tie, return which has more stars</span>
   <span style="color: #4080ff;">if</span> <span style="color: grey;">&#40;</span><span style="color: #ffc080;">$a_stars</span> <span style="color: grey;">!=</span> <span style="color: #ffc080;">$b_stars</span><span style="color: grey;">&#41;</span> <span style="color: grey;">&#123;</span>
      <span style="color: #4080ff;">return</span> <span style="color: grey;">&#40;</span><span style="color: grey;">&#40;</span>float<span style="color: grey;">&#41;</span><span style="color: #ffc080;">$a_stars</span> <span style="color: grey;">&gt;</span> <span style="color: grey;">&#40;</span>float<span style="color: grey;">&#41;</span><span style="color: #ffc080;">$b_stars</span><span style="color: grey;">&#41;</span> ? <span style="color: grey;">-</span><span style="color: #cc66cc;">1</span> <span style="color: grey;">:</span> <span style="color: #cc66cc;">1</span><span style="color: grey;">;</span>
   <span style="color: grey;">&#125;</span>
   <span style="color: #80ff80">// else, if they are tied, go to the priority tiebreaker...</span>
&nbsp;
   <span style="color: #80ff80">// get the priority value for each</span>
   <span style="color: #ffc080;">$a_priority</span> <span style="color: grey;">=</span> get_post_meta<span style="color: grey;">&#40;</span><span style="color: #ffc080;">$a</span><span style="color: grey;">-&gt;</span><span style="color: magenta;">ID</span><span style="color: grey;">,</span> <span style="color: #0000ff;">'rating-priority'</span><span style="color: grey;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: grey;">&#41;</span><span style="color: grey;">;</span>
   <span style="color: #ffc080;">$b_priority</span> <span style="color: grey;">=</span> get_post_meta<span style="color: grey;">&#40;</span><span style="color: #ffc080;">$b</span><span style="color: grey;">-&gt;</span><span style="color: magenta;">ID</span><span style="color: grey;">,</span> <span style="color: #0000ff;">'rating-priority'</span><span style="color: grey;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: grey;">&#41;</span><span style="color: grey;">;</span>
&nbsp;
   <span style="color: #80ff80">// if a priority value has not been entered, default to 0</span>
   <span style="color: #ffc080;">$a_priority</span> <span style="color: grey;">=</span> <span style="color: grey;">&#40;</span><span style="color: #ffc080;">$a_priority</span> <span style="color: grey;">==</span> <span style="color: #0000ff;">''</span><span style="color: grey;">&#41;</span> ? <span style="color: #cc66cc;">0</span> <span style="color: grey;">:</span> <span style="color: grey;">&#40;</span>int<span style="color: grey;">&#41;</span><span style="color: #ffc080;">$a_priority</span><span style="color: grey;">;</span>
   <span style="color: #ffc080;">$b_priority</span> <span style="color: grey;">=</span> <span style="color: grey;">&#40;</span><span style="color: #ffc080;">$b_priority</span> <span style="color: grey;">==</span> <span style="color: #0000ff;">''</span><span style="color: grey;">&#41;</span> ? <span style="color: #cc66cc;">0</span> <span style="color: grey;">:</span> <span style="color: grey;">&#40;</span>int<span style="color: grey;">&#41;</span><span style="color: #ffc080;">$b_priority</span><span style="color: grey;">;</span>
&nbsp;
   <span style="color: #80ff80">// if the priority is also equal, just return as a tie</span>
   <span style="color: #4080ff;">if</span> <span style="color: grey;">&#40;</span><span style="color: #ffc080;">$a_priority</span> <span style="color: grey;">==</span> <span style="color: #ffc080;">$b_priority</span><span style="color: grey;">&#41;</span> <span style="color: grey;">&#123;</span>
      <span style="color: #4080ff;">return</span> <span style="color: #cc66cc;">0</span><span style="color: grey;">;</span>
   <span style="color: grey;">&#125;</span>
   <span style="color: #80ff80">// if not, we return the priority comparison</span>
   <span style="color: #4080ff;">return</span> <span style="color: grey;">&#40;</span><span style="color: #ffc080;">$a_priority</span> <span style="color: grey;">&lt;</span> <span style="color: #ffc080;">$b_priority</span><span style="color: grey;">&#41;</span> ? <span style="color: grey;">-</span><span style="color: #cc66cc;">1</span> <span style="color: grey;">:</span> <span style="color: #cc66cc;">1</span><span style="color: grey;">;</span>
<span style="color: grey;">&#125;</span></pre></td></tr></table></div>

<h3>How the Tie-Breaker Function Works</h3>
<p>The way the custom sort function works is by comparing two elements of the array ($a and $b, above), and deciding which one is &#8220;greater than&#8221; the other.  If $a is greater than $b, we should return a value of 1.  If the opposite is true, we return -1.  If they are tied, we return 0.  The above function uses ternary operators to compare first the star ratings and then the priority values.  If the stars are not tied, it just returns which has the higher star rating, and never gets to the tie-breaker phase.  But if they are equal, we then compare the second criteria.</p>
<p>There is also a check to make sure the &#8220;priority&#8221; field has a value.  This is so that the user is not forced to input a value for every single review, just the ones that need to be tweaked.  Using this system, all that is required to bump a review up above another review with an equal star-rating is to insert a &#8220;-1&#8243; in the priority box.</p>
<p>I used this for a star rating system, but it could be used for any number of applications.  For example, if you wanted to order your posts by author, but for each author make sure they are in chronological order.</p>
]]></content:encoded>
			<wfw:commentRss>http://jeffgran.com/218/blog/wordpress-sort-posts-by-multiple-fields/feed</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
		<item>
		<title>WordPress: Tabbed Sidebar Widgets with jQuery</title>
		<link>http://jeffgran.com/180/blog/wordpress-tabbed-sidebar-widgets-with-jquery</link>
		<comments>http://jeffgran.com/180/blog/wordpress-tabbed-sidebar-widgets-with-jquery#comments</comments>
		<pubDate>Wed, 01 Jul 2009 22:33:13 +0000</pubDate>
		<dc:creator>Jeff Gran</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://jeffgran.com/?p=180</guid>
		<description><![CDATA[Tabs are a great way to show more information in less space, assuming the initially-hidden tabs are non-essential information.  Recently a client asked me to put a few Wordpress widgets into a single tabbed container in the sidebar.  Here's how I did it.]]></description>
			<content:encoded><![CDATA[<p>Tabs are a great way to show more information in less space, assuming the initially-hidden tabs are non-essential information.  Recently a client asked me to put a few WordPress widgets into a single tabbed container in the sidebar.  Here&#8217;s how I did it.</p>
<h3>The Problem</h3>
<p>There&#8217;s no reason to re-invent the wheel, and there are already several tabs implementations written as jQuery plugins, including the <a title="jQuery UI Tabs" href="http://jqueryui.com/demos/tabs/">jQuery UI tabs widget</a>. The problem with these is that most (if not all) of them require the markup to be set up in a different way than we have available with our WordPress-outputted sidebar markup.</p>
<p>To illustrate, WordPress by default outputs the sidebar with markup that is structured something like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="html4strict" style="color: #ccc;"><span style="color: #009900;">&lt;<span style="color: #ffff00;">div</span> <span style="color: cyan;">id</span><span style="color: grey;">=</span><span style="color: #ff8080;">&quot;sidebar-1&quot;</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #ffff00;">ul</span> <span style="color: cyan;">class</span><span style="color: grey;">=</span><span style="color: #ff8080;">&quot;sidebar-list&quot;</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #ffff00;">li</span> <span style="color: cyan;">id</span><span style="color: grey;">=</span><span style="color: #ff8080;">&quot;widget-1&quot;</span> <span style="color: cyan;">class</span><span style="color: grey;">=</span><span style="color: #ff8080;">&quot;widget widget-class-1&quot;</span>&gt;</span>
      <span style="color: #009900;">&lt;<span style="color: #ffff00;">h2</span>&gt;</span>Widget 1 Title<span style="color: #009900;">&lt;<span style="color: grey;">/</span><span style="color: #ffff00;">h2</span>&gt;</span>
      <span style="color: #009900;">&lt;<span style="color: #ffff00;">p</span>&gt;</span>...content...<span style="color: #009900;">&lt;<span style="color: grey;">/</span><span style="color: #ffff00;">p</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: grey;">/</span><span style="color: #ffff00;">li</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #ffff00;">li</span> <span style="color: cyan;">id</span><span style="color: grey;">=</span><span style="color: #ff8080;">&quot;widget-2&quot;</span> <span style="color: cyan;">class</span><span style="color: grey;">=</span><span style="color: #ff8080;">&quot;widget widget-class-2&quot;</span>&gt;</span>
      <span style="color: #009900;">&lt;<span style="color: #ffff00;">h2</span>&gt;</span>Widget 2 Title<span style="color: #009900;">&lt;<span style="color: grey;">/</span><span style="color: #ffff00;">h2</span>&gt;</span>
      <span style="color: #009900;">&lt;<span style="color: #ffff00;">p</span>&gt;</span>...content2...<span style="color: #009900;">&lt;<span style="color: grey;">/</span><span style="color: #ffff00;">p</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: grey;">/</span><span style="color: #ffff00;">li</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: grey;">/</span><span style="color: #ffff00;">ul</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: grey;">/</span><span style="color: #ffff00;">div</span>&gt;</span></pre></td></tr></table></div>

<p>The various jQuery tabs plugins all require the markup to be strutured like this (notice the titles moved to the additional list at the top, with anchor tags corresponding to the ids of the respective widgets:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="html4strict" style="color: #ccc;"><span style="color: #009900;">&lt;<span style="color: #ffff00;">div</span> <span style="color: cyan;">id</span><span style="color: grey;">=</span><span style="color: #ff8080;">&quot;sidebar-1&quot;</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #ffff00;">ul</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #ffff00;">li</span>&gt;&lt;<span style="color: #ffff00;">a</span> <span style="color: cyan;">href</span><span style="color: grey;">=</span><span style="color: #ff8080;">&quot;#widget-1&quot;</span>&gt;</span>Widget 1 Title<span style="color: #009900;">&lt;<span style="color: grey;">/</span><span style="color: #ffff00;">a</span>&gt;&lt;<span style="color: grey;">/</span><span style="color: #ffff00;">li</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #ffff00;">li</span>&gt;&lt;<span style="color: #ffff00;">a</span> <span style="color: cyan;">href</span><span style="color: grey;">=</span><span style="color: #ff8080;">&quot;#widget-2&quot;</span>&gt;</span>Widget 2 Title<span style="color: #009900;">&lt;<span style="color: grey;">/</span><span style="color: #ffff00;">a</span>&gt;&lt;<span style="color: grey;">/</span><span style="color: #ffff00;">li</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: grey;">/</span><span style="color: #ffff00;">ul</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: #ffff00;">ul</span> <span style="color: cyan;">class</span><span style="color: grey;">=</span><span style="color: #ff8080;">&quot;sidebar-list&quot;</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #ffff00;">li</span> <span style="color: cyan;">id</span><span style="color: grey;">=</span><span style="color: #ff8080;">&quot;widget-1&quot;</span> <span style="color: cyan;">class</span><span style="color: grey;">=</span><span style="color: #ff8080;">&quot;widget widget-class-1&quot;</span>&gt;</span>
      <span style="color: #009900;">&lt;<span style="color: #ffff00;">p</span>&gt;</span>...content...<span style="color: #009900;">&lt;<span style="color: grey;">/</span><span style="color: #ffff00;">p</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: grey;">/</span><span style="color: #ffff00;">li</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #ffff00;">li</span> <span style="color: cyan;">id</span><span style="color: grey;">=</span><span style="color: #ff8080;">&quot;widget-2&quot;</span> <span style="color: cyan;">class</span><span style="color: grey;">=</span><span style="color: #ff8080;">&quot;widget widget-class-2&quot;</span>&gt;</span>
      <span style="color: #009900;">&lt;<span style="color: #ffff00;">p</span>&gt;</span>...content2...<span style="color: #009900;">&lt;<span style="color: grey;">/</span><span style="color: #ffff00;">p</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: grey;">/</span><span style="color: #ffff00;">li</span>&gt;</span>
  <span style="color: #009900;">&lt;<span style="color: grey;">/</span><span style="color: #ffff00;">ul</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: grey;">/</span><span style="color: #ffff00;">div</span>&gt;</span></pre></td></tr></table></div>

<h3>The Solution</h3>
<p>While it would be possible to alter the PHP code to output the list above the widgets (although, I looked into this solution and was not able to find a hook that would allow it without altering the core), we can just use some custom jQuery to dynamically add the list before invoking the tabs plugin.  This method also has the advantage that it makes our tabs more unobtrusive.  If javascript is disabled, the page will look exactly as it would with the standard sidebar widgets, but if javascript is available, we&#8217;ll fix the markup and add the tabs all at once with our jQuery code.</p>
<h3>The Code</h3>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
</pre></td><td class="code"><pre class="javascript" style="color: #ccc;">$<span style="color: grey;">&#40;</span>document<span style="color: grey;">&#41;</span>.<span style="color: magenta;">ready</span><span style="color: grey;">&#40;</span><span style="color: #ffff00;">function</span><span style="color: grey;">&#40;</span><span style="color: grey;">&#41;</span> <span style="color: grey;">&#123;</span>
   <span style="color: #80ff80">// first, we select the sidebar</span>
   $<span style="color: grey;">&#40;</span><span style="color: #ff8080;">'#sidebar-1 .sidebar-list'</span><span style="color: grey;">&#41;</span>.<span style="color: magenta;">each</span><span style="color: grey;">&#40;</span><span style="color: #ffff00;">function</span><span style="color: grey;">&#40;</span><span style="color: grey;">&#41;</span> <span style="color: grey;">&#123;</span>
      <span style="color: #80ff80">// each child of the sidebar element will be a widget, and will become a tab</span>
      <span style="color: #ffff00;">var</span> widgets <span style="color: grey;">=</span> $<span style="color: grey;">&#40;</span><span style="color: #4080ff;">this</span><span style="color: grey;">&#41;</span>.<span style="color: magenta;">children</span><span style="color: grey;">&#40;</span><span style="color: grey;">&#41;</span><span style="color: grey;">;</span>
&nbsp;
      <span style="color: #80ff80">// we'll build the list of titles, starting with the opening tag</span>
      <span style="color: #ffff00;">var</span> titleList <span style="color: grey;">=</span> <span style="color: #ff8080;">'&lt;ul&gt;'</span><span style="color: grey;">;</span>
&nbsp;
      <span style="color: #80ff80">//for each widget in the sidebar:</span>
      widgets.<span style="color: magenta;">each</span><span style="color: grey;">&#40;</span><span style="color: #ffff00;">function</span><span style="color: grey;">&#40;</span><span style="color: grey;">&#41;</span> <span style="color: grey;">&#123;</span>
&nbsp;
         <span style="color: #80ff80">// save the title of the widget</span>
         <span style="color: #ffff00;">var</span> widgetTitle <span style="color: grey;">=</span> $<span style="color: grey;">&#40;</span><span style="color: #4080ff;">this</span><span style="color: grey;">&#41;</span>.<span style="color: magenta;">children</span><span style="color: grey;">&#40;</span><span style="color: #ff8080;">'h2'</span><span style="color: grey;">&#41;</span>.<span style="color: magenta;">text</span><span style="color: grey;">&#40;</span><span style="color: grey;">&#41;</span><span style="color: grey;">;</span>
         <span style="color: #80ff80">// then hide it since it will be displayed in the tab</span>
         $<span style="color: grey;">&#40;</span><span style="color: #4080ff;">this</span><span style="color: grey;">&#41;</span>.<span style="color: magenta;">children</span><span style="color: grey;">&#40;</span><span style="color: #ff8080;">'h2'</span><span style="color: grey;">&#41;</span>.<span style="color: magenta;">hide</span><span style="color: grey;">&#40;</span><span style="color: grey;">&#41;</span><span style="color: grey;">;</span>
&nbsp;
         <span style="color: #80ff80">// create a new list item for the tab, linking to this widget's id</span>
         <span style="color: #ffff00;">var</span> listItem <span style="color: grey;">=</span> <span style="color: #ff8080;">'&lt;li&gt;&lt;a href=&quot;#'</span><span style="color: grey;">+</span>$<span style="color: grey;">&#40;</span><span style="color: #4080ff;">this</span><span style="color: grey;">&#41;</span>.<span style="color: magenta;">attr</span><span style="color: grey;">&#40;</span><span style="color: #ff8080;">'id'</span><span style="color: grey;">&#41;</span><span style="color: grey;">+</span><span style="color: #ff8080;">'&quot;&gt;'</span><span style="color: grey;">+</span>widgetTitle<span style="color: grey;">+</span><span style="color: #ff8080;">'&lt;/a&gt;&lt;/li&gt;'</span><span style="color: grey;">;</span>
         <span style="color: #80ff80">// add the list item to the list we're building</span>
         titleList <span style="color: grey;">+=</span> listItem<span style="color: grey;">;</span>
      <span style="color: grey;">&#125;</span><span style="color: grey;">&#41;</span><span style="color: grey;">;</span>
      <span style="color: #80ff80">// close the list now that we're done going through each widget</span>
      titleList <span style="color: grey;">+=</span> <span style="color: #ff8080;">'&lt;/ul&gt;'</span><span style="color: grey;">;</span>
&nbsp;
      <span style="color: #80ff80">// add the title list to the beginning of the sidebar</span>
      $<span style="color: grey;">&#40;</span><span style="color: #4080ff;">this</span><span style="color: grey;">&#41;</span>.<span style="color: magenta;">before</span><span style="color: grey;">&#40;</span>titleList<span style="color: grey;">&#41;</span><span style="color: grey;">;</span>
   <span style="color: grey;">&#125;</span><span style="color: grey;">&#41;</span><span style="color: grey;">;</span>
&nbsp;
   <span style="color: #80ff80">// apply the tabs plugin</span>
   $<span style="color: grey;">&#40;</span><span style="color: #ff8080;">'#sidebar-1'</span><span style="color: grey;">&#41;</span>.<span style="color: magenta;">tabs</span><span style="color: grey;">&#40;</span><span style="color: grey;">&#41;</span><span style="color: grey;">;</span>
<span style="color: grey;">&#125;</span><span style="color: grey;">&#41;</span><span style="color: grey;">;</span></pre></td></tr></table></div>

<h3>Explanation</h3>
<p>The comments in the code should make the solution pretty self-explanatory.  This will take all the widgets in the selected sidebar and create tabs for them.</p>
<p>There are some things you&#8217;ll have to be careful about if you use this code.  First, you&#8217;ll have to change the selection for the sidebar div (line 3 above, as well as in the second-to-last line) to match yours.  This assumes you <em>have</em> your sidebar list wrapped in a div (required by the tabs plugin), so if you don&#8217;t, you will need to add that, either in the theme itself or with jQuery( using .wrap() ). Also, in the two places you see &#8216;h2&#8242;, this is assuming that the titles of the widgets are in &lt;h2&gt; tags.  You may need to select a different tag if your theme displays the widget titles differently.</p>
<p>Also note that this will create tabs for <em>all</em> the widgets in the sidebar.  Making it only apply to specific widgets is possible, but a bit more complicated.  Luckily, with WordPress 2.8, the sidebars/widgets interface is much improved.  One solution would be to create multiple &#8216;sidebars&#8217; that stacked on top of each other in the sidebar area of your theme, and apply the above tab-ifying code to one or more of them individually.</p>
]]></content:encoded>
			<wfw:commentRss>http://jeffgran.com/180/blog/wordpress-tabbed-sidebar-widgets-with-jquery/feed</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Site Upgrades: WordPress 2.8!</title>
		<link>http://jeffgran.com/163/blog/site-upgrades-wordpress-28</link>
		<comments>http://jeffgran.com/163/blog/site-upgrades-wordpress-28#comments</comments>
		<pubDate>Sat, 13 Jun 2009 00:17:34 +0000</pubDate>
		<dc:creator>Jeff Gran</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[announcement]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://jeffgran.com/?p=163</guid>
		<description><![CDATA[WordPress 2.8 was released on Wednesday, and I've upgraded my site.]]></description>
			<content:encoded><![CDATA[<p>WordPress 2.8 <a title="WordPress 2.8 Jazzes Themes and Widgets" href="http://wordpress.org/development/2009/06/wordpress-28/">was released</a> on Wednesday, and I&#8217;ve upgraded my site.  I&#8217;m looking forward to playing with the new widgets API.  Hmm&#8230; what kind of widgets will I make?</p>
<p>I also upgraded to the latest <a title="Pods: WordPress CMS Plugin" href="http://pods.uproot.us/">Pods</a> release (1.6.5), which will allow some nifty behind-the-scenes magic to happen.</p>
<p>That&#8217;s all.  Carry on.</p>
]]></content:encoded>
			<wfw:commentRss>http://jeffgran.com/163/blog/site-upgrades-wordpress-28/feed</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>WordPress: Using a custom home page</title>
		<link>http://jeffgran.com/139/blog/wordpress-custom-home-page</link>
		<comments>http://jeffgran.com/139/blog/wordpress-custom-home-page#comments</comments>
		<pubDate>Tue, 19 May 2009 19:25:13 +0000</pubDate>
		<dc:creator>Jeff Gran</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://jeffgran.com/?p=139</guid>
		<description><![CDATA[WordPress hs a simple checkbox in the Admin->Settings panel that lets you set a certain file to be the "static" home page instead of the default listing of the latest posts.

I'm going to show you how (and why) I did NOT use that feature to create my customized home page.]]></description>
			<content:encoded><![CDATA[<p>WordPress hs a simple checkbox in the Admin-&gt;Settings panel that lets you set a certain file to be the &#8220;static&#8221; home page instead of the default listing of the latest posts.</p>
<p>I&#8217;m going to show you how (and why) I did NOT use that feature to create my customized home page.</p>
<p>Here&#8217;s what I wanted:</p>
<ol>
<li>A unique page for the home page, including a few of the latest posts (displayed differently than on the main blog page)</li>
<li>The main blog page to be located at &#8220;url.com/blog&#8221;, showing the default latest posts  (assuming permalinks are turned on)</li>
<li>All other dynamic archive pages shown by month, tag, category, etc., should be shown using the same template as the main blog page, without creating any duplicate files</li>
</ol>
<p>Here&#8217;s how I did it:</p>
<h3>home.php</h3>
<p>Instead of using the admin interface to designate a specific file for your home page, you can use WordPress&#8217;s built-in <a title="Template Hierarchy" href="http://codex.wordpress.org/Template_Hierarchy">template hierarchy</a>.  This method has the advantage that if you&#8217;re building this theme for the public, it will automatically behave how you want it to, instead of having to instruct the user to alter their settings.</p>
<p>To do this simply design your home page&#8217;s php/html/css, and name it home.php.  That&#8217;s it &#8211;  &#8220;home.php&#8221; takes precedence over &#8220;index.php&#8221;.</p>
<h3>Custom loop</h3>
<p>WordPress shows blog posts in what they call &#8220;The Loop&#8221;.  The Loop works by doing the following:</p>
<ol>
<li>The query &#8212; gets a set of posts based on what page we&#8217;re viewing.  Usually this is automatic, using the URL (e.g. &#8220;?p=138&#8243; or &#8220;/category/news&#8221;).</li>
<li>&#8220;if(have_posts()):&#8221; &#8212; just like it looks, it makes sure the query got at least one post.</li>
<li>&#8220;while(have_posts()):&#8221; &#8212; keep doing the loop until we don&#8217;t have any more posts.</li>
<li>&#8220;the_post();&#8221; &#8212; this basically &#8220;loads up&#8221; the current post in the loop so that all the <a title="Template Tags" href="http://codex.wordpress.org/Template_Tags">template tags</a> associated with the post will reference this specific post from this specific query.</li>
<li>Then comes the markup output for each post.</li>
<li>&#8220;endwhile; endif;&#8221; &#8212; the end of the loop.</li>
</ol>
<p>So, in order to customize the Loop, there are two main things we can do.  One is to make a unique query, and the other is to create a unique layout structure inside the loop.</p>
<p>For my home page, I wanted only the 2 most recent posts rather than the number set in the admin panel that would be used for the main blog page.  I added a simple counter and put this code in the appropriate spot in home.php:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="php" style="color: #ccc;"><span style="color: #ffc080;">$i</span> <span style="color: grey;">=</span> <span style="color: #cc66cc;">0</span><span style="color: grey;">;</span>
<span style="color: #4080ff;">if</span><span style="color: grey;">&#40;</span>have_posts<span style="color: grey;">&#40;</span><span style="color: grey;">&#41;</span><span style="color: grey;">&#41;</span> <span style="color: grey;">:</span> <span style="color: #4080ff;">while</span><span style="color: grey;">&#40;</span>have_posts<span style="color: grey;">&#40;</span><span style="color: grey;">&#41;</span> <span style="color: grey;">&amp;&amp;</span> <span style="color: #ffc080;">$i</span><span style="color: grey;">&lt;</span><span style="color: #cc66cc;">2</span><span style="color: grey;">&#41;</span> <span style="color: grey;">:</span> the_post<span style="color: grey;">&#40;</span><span style="color: grey;">&#41;</span><span style="color: grey;">;</span>
   <span style="color: #80ff80">// html output, template tags, etc go here</span>
   <span style="color: #ffc080;">$i</span><span style="color: grey;">++;</span>
<span style="color: #4080ff;">endwhile</span><span style="color: grey;">;</span> <span style="color: #4080ff;">endif</span><span style="color: grey;">;</span></pre></td></tr></table></div>

<h3>index.php</h3>
<p>In the aforementioned template hierarchy, index.php is the default template for all blog pages, and in the absence of home.php it would be the home page as well.  So I used index.php to put my main, default loop.  But how do we get index.php to show up at &#8220;url.com/blog&#8221;?</p>
<p>This is the part that I have never seen anywhere else, and what prompted me to write this post.  What I did was use index.php for its special status in the template hierarchy, and ALSO used the same file as a Page Template.  To use index.php as a template, just add the magic keyword/comment at the top, like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="php" style="color: #ccc;"><span style="color: #ffff00;">&lt;?php</span> <span style="color: #80ff80">/*
Template Name: blog
 */</span></pre></td></tr></table></div>

<p>Now, create a blank Page in the admin panel titled Blog, and assign the &#8216;blog&#8217; page template to it.  The title will still be used to display the title on the page, and assigning the &#8216;blog&#8217; template to it makes it use index.php as the page template.</p>
<h3>Custom query</h3>
<p>So now the url for the blog is right, but it&#8217;s displaying the blank page created in the admin instead of the default loop of latest blog posts.  To remedy that, we put a conditional test at the top, and if this is the blog page, we &#8220;reset&#8221; the query so that it contains all the posts.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="php" style="color: #ccc;"><span style="color: #4080ff;">if</span><span style="color: grey;">&#40;</span>is_page<span style="color: grey;">&#40;</span><span style="color: #0000ff;">'blog'</span><span style="color: grey;">&#41;</span><span style="color: grey;">&#41;</span> <span style="color: grey;">&#123;</span>
   query_posts<span style="color: grey;">&#40;</span><span style="color: #ff8080;">&quot;&quot;</span><span style="color: grey;">&#41;</span><span style="color: grey;">;</span>
<span style="color: grey;">&#125;</span></pre></td></tr></table></div>

<p>The &#8220;query_posts&#8221; function creates the query, as mentioned above.  Passing an empty string as the argument makes sure we get all the posts.  We put this above the start of the Loop, to override the automatic query that is just returning the one &#8216;post&#8217;, in this case being the blank Blog page.</p>
<h3>Fixing the pagination</h3>
<p>There&#8217;s one other quirky problem we have to deal with now.  The flaw is in the empty query.  It works, but when you get to multiple pages worth of posts, the paging will not work right.  When we click the &#8220;next page&#8221; or &#8220;page 2&#8243; link, the page number we&#8217;re going gets passed to the database as part of the query.  When we override the query with a blank one, we also override the page information, so we always end up on page 1.  To fix this, we have to find add a little test to find out the proper page, and add that in to our custom query.  After adding the paging fix, our custom query looks like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="php" style="color: #ccc;"><span style="color: #4080ff;">if</span><span style="color: grey;">&#40;</span>is_page<span style="color: grey;">&#40;</span><span style="color: #0000ff;">'blog'</span><span style="color: grey;">&#41;</span><span style="color: grey;">&#41;</span> <span style="color: grey;">&#123;</span>
   <span style="color: #4080ff;">if</span> <span style="color: grey;">&#40;</span>get_query_var<span style="color: grey;">&#40;</span><span style="color: #0000ff;">'paged'</span><span style="color: grey;">&#41;</span><span style="color: grey;">&#41;</span>
      <span style="color: #ffc080;">$paged</span> <span style="color: grey;">=</span> get_query_var<span style="color: grey;">&#40;</span><span style="color: #0000ff;">'paged'</span><span style="color: grey;">&#41;</span><span style="color: grey;">;</span>
   <span style="color: #4080ff;">else</span> <span style="color: #ffc080;">$paged</span> <span style="color: grey;">=</span> <span style="color: #cc66cc;">1</span><span style="color: grey;">;</span>
   query_posts<span style="color: grey;">&#40;</span><span style="color: #ff8080;">&quot;paged=<span style="color: #006699; font-weight: bold;">$paged</span>&quot;</span><span style="color: grey;">&#41;</span><span style="color: grey;">;</span>
<span style="color: grey;">&#125;</span></pre></td></tr></table></div>

<p>&#8220;get_query_var(&#8216;paged&#8217;)&#8221; gets the paging information from the original query that we&#8217;re about to override.  If it&#8217;s there, we use it, and if not, we assume we&#8217;re on page one.  Now you can write a spiffy Loop below this and it will be used for all blog post listings, including the default one at &#8220;/blog&#8221;</p>
<p>I hope this was helpful to someone!</p>
]]></content:encoded>
			<wfw:commentRss>http://jeffgran.com/139/blog/wordpress-custom-home-page/feed</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Site Theme Upgrade</title>
		<link>http://jeffgran.com/134/blog/site-theme-upgrade</link>
		<comments>http://jeffgran.com/134/blog/site-theme-upgrade#comments</comments>
		<pubDate>Thu, 14 May 2009 23:34:25 +0000</pubDate>
		<dc:creator>Jeff Gran</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[announcement]]></category>
		<category><![CDATA[design]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://jeffgran.com/?p=134</guid>
		<description><![CDATA[I've "gone live" with the theme upgrade I've been working on.  This represents a week's worth of work or so.]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve &#8220;gone live&#8221; with the theme upgrade I&#8217;ve been working on.  This represents a week&#8217;s worth of work or so.  I added many new features, including a custom front page, a widgetized sidebar, and a custom sidebar widget displaying my social media, email and rss links (with custom-created buttons, of course).</p>
<p>The portfolio section is empty as of now, as I am in development of a new structure for it using the <a title="Pods: WordPress CMS Plugin" href="http://pods.uproot.us/">Pods CMS plugin</a> for WordPress.</p>
]]></content:encoded>
			<wfw:commentRss>http://jeffgran.com/134/blog/site-theme-upgrade/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Specialize, they say</title>
		<link>http://jeffgran.com/119/blog/specialize-they-say</link>
		<comments>http://jeffgran.com/119/blog/specialize-they-say#comments</comments>
		<pubDate>Wed, 06 May 2009 21:40:01 +0000</pubDate>
		<dc:creator>Jeff Gran</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[announcement]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://jeffgran.com/?p=119</guid>
		<description><![CDATA[I&#8217;ve been reading a lot about how to make your way in this crazy world of people who make web sites.  Several authorities have suggested that, especially when starting out, it&#8217;s a good idea to specialize in one particular niche. I&#8217;ve decided to concentrate on WordPress development for several reasons: WordPress is one of the [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been reading a lot about how to make your way in this crazy world of people who make web sites.  Several authorities have suggested that, especially when starting out, it&#8217;s a good idea to specialize in one particular niche.</p>
<p>I&#8217;ve decided to concentrate on WordPress development for several reasons:</p>
<ul>
<li>WordPress is one of the most popular blogging and CMS systems out there today.</li>
<li>In the hands of an expert, WP can be extremely flexible.</li>
<li>WP is easy to get started with, but difficult to master.  Inexperienced users often require expert assistance in customization.</li>
<li>I&#8217;m already using it for this site, and have at least one more site in development using it.</li>
<li>The skills used in creating wordpress themes (XHTML, CSS, PHP) are all applicable to generic web design uses &#8211; I&#8217;m not boxing myself in.</li>
</ul>
<p>The plan for now is to keep practicing with custom wordpress themes.  First up is a redesign of this site.  Yep, it&#8217;s only a couple of weeks old and it&#8217;s already on the chopping block.  What can I say?  Sometimes I&#8217;m a perfectionist.</p>
]]></content:encoded>
			<wfw:commentRss>http://jeffgran.com/119/blog/specialize-they-say/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>WordPress: Custom Links Display</title>
		<link>http://jeffgran.com/38/blog/wordpress-custom-links-display</link>
		<comments>http://jeffgran.com/38/blog/wordpress-custom-links-display#comments</comments>
		<pubDate>Fri, 24 Apr 2009 09:53:36 +0000</pubDate>
		<dc:creator>Jeff Gran</dc:creator>
				<category><![CDATA[Blog]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://jeffgran.com/?p=38</guid>
		<description><![CDATA[I&#8217;ve been working with WordPress for a couple of days now, and I&#8217;m thoroughly impressed by its elegant design. I&#8217;ve always been a fan of modular, extensible, customizable systems, and WordPress does not disappoint. The great thing about the architecture they&#8217;ve built is that if one level of control can&#8217;t do what you need, there [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been working with WordPress for a couple of days now, and I&#8217;m thoroughly impressed by its elegant design. I&#8217;ve always been a fan of modular, extensible, customizable systems, and WordPress does not disappoint. The great thing about the architecture they&#8217;ve built is that if one level of control can&#8217;t do what you need, there is always another successive level to delve into.</p>
<p>My point will be made more easily with an example:  I wanted to show the links on my &#8220;Links&#8221; page in boxes, with linked title and images, and a short description, all controlled by CSS classes.</p>
<p>WordPress offers a &#8220;template tag&#8221; (that is, PHP function) for theme creators to display the list of links added via the admin panel.  You can put it anywhere in your templates (for example, in the sidebar) and it will automatically keep the list up-to-date:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="color: #ccc;">wp_list_bookmarks<span style="color: grey;">&#40;</span><span style="color: grey;">&#41;</span><span style="color: grey;">;</span></pre></td></tr></table></div>

<p>That function generates an unordered list of link categories, with sub-lists of each link, complete with CSS styles assigned so that you can add a custom style.  But, say you (like me) don&#8217;t want the categories, just the links please.  And can I have it show the images associated with the links as well?</p>
<p>To this end, the WordPress developers have included a large range of optional arguments for the function above, so you can include or exclude categories, decide whether to format the list as an unordered list (&lt;ul&gt;) or other custom HTML tag, whether to show the images and descriptions, etc:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="php" style="color: #ccc;">wp_list_bookmarks<span style="color: grey;">&#40;</span><span style="color: #0000ff;">'categorize=0&amp;title_li=&amp;show_images=1&amp;show_description=1'</span><span style="color: grey;">&#41;</span><span style="color: grey;">;</span></pre></td></tr></table></div>

<p>Pretty good, but what if I want even more control (I usually do <img src='http://jeffgran.com/wp-includes/images/smilies/icon_wink.gif' alt=';)' class='wp-smiley' />  )?  The next-deepest level of under-the-hood-ness allows you to get the list of links, store it in an array, and then loop through the array using a simple PHP foreach loop that outputs the link data in whatever custom format you need.  It&#8217;s easier than it sounds.  Here&#8217;s the final code I used for my links page:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
</pre></td><td class="code"><pre class="php" style="color: #ccc;"><span style="color: #ffff00;">&lt;?php</span>
<span style="color: #ffc080;">$myLinks</span> <span style="color: grey;">=</span> get_bookmarks<span style="color: grey;">&#40;</span><span style="color: grey;">&#41;</span><span style="color: grey;">;</span>
<span style="color: #4080ff;">foreach</span><span style="color: grey;">&#40;</span><span style="color: #ffc080;">$myLinks</span> <span style="color: #4080ff;">as</span> <span style="color: #ffc080;">$myLink</span><span style="color: grey;">&#41;</span> <span style="color: grey;">&#123;</span>
<span style="color: grey;">?&gt;</span>
   &lt;div class=&quot;linkBox&quot;&gt;
      &lt;a href=&quot;<span style="color: #ffff00;">&lt;?php</span> <span style="color: #4080ff;">echo</span><span style="color: grey;">&#40;</span><span style="color: #ffc080;">$myLink</span><span style="color: grey;">-&gt;</span><span style="color: magenta;">link_url</span><span style="color: grey;">&#41;</span><span style="color: grey;">;</span> <span style="color: grey;">?&gt;</span>&quot;&gt;&lt;img src=&quot;<span style="color: #ffff00;">&lt;?php</span> <span style="color: #4080ff;">echo</span><span style="color: grey;">&#40;</span><span style="color: #ffc080;">$myLink</span><span style="color: grey;">-&gt;</span><span style="color: magenta;">link_image</span><span style="color: grey;">&#41;</span><span style="color: grey;">;</span> <span style="color: grey;">?&gt;</span>&quot; /&gt;&lt;/a&gt;
      &lt;h3&gt;&lt;a href=&quot;<span style="color: #ffff00;">&lt;?php</span> <span style="color: #4080ff;">echo</span><span style="color: grey;">&#40;</span><span style="color: #ffc080;">$myLink</span><span style="color: grey;">-&gt;</span><span style="color: magenta;">link_url</span><span style="color: grey;">&#41;</span><span style="color: grey;">;</span> <span style="color: grey;">?&gt;</span>&quot;&gt;<span style="color: #ffff00;">&lt;?php</span> <span style="color: #4080ff;">echo</span><span style="color: grey;">&#40;</span><span style="color: #ffc080;">$myLink</span><span style="color: grey;">-&gt;</span><span style="color: magenta;">link_name</span><span style="color: grey;">&#41;</span><span style="color: grey;">;</span> <span style="color: grey;">?&gt;</span>&lt;/a&gt;&lt;/h3&gt;
      &lt;p&gt;<span style="color: #ffff00;">&lt;?php</span> <span style="color: #4080ff;">echo</span><span style="color: grey;">&#40;</span><span style="color: #ffc080;">$myLink</span><span style="color: grey;">-&gt;</span><span style="color: magenta;">link_description</span><span style="color: grey;">&#41;</span><span style="color: grey;">;</span> <span style="color: grey;">?&gt;</span>&lt;/p&gt;
   &lt;/div&gt;
<span style="color: #ffff00;">&lt;?php</span> <span style="color: grey;">&#125;</span> <span style="color: grey;">?&gt;</span></pre></td></tr></table></div>

<p>And of course, if needed there are more levels below this one.  You could do a loop within a loop, for example, to format each category of links with its own category heading, different colors or styles, etc.</p>
<p>Ahhh, the power! <img src='http://jeffgran.com/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://jeffgran.com/38/blog/wordpress-custom-links-display/feed</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

