<?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>Aldosoft &#187; How To</title>
	<atom:link href="http://aldosoft.com/blog/category/how-to/feed/" rel="self" type="application/rss+xml" />
	<link>http://aldosoft.com</link>
	<description>We build crisp, intuitive web-based systems</description>
	<lastBuildDate>Mon, 04 Apr 2011 05:16: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>Thesis Custom Loop Starter Template</title>
		<link>http://aldosoft.com/blog/2011/01/thesis-custom-loop-template/</link>
		<comments>http://aldosoft.com/blog/2011/01/thesis-custom-loop-template/#comments</comments>
		<pubDate>Thu, 20 Jan 2011 21:30:00 +0000</pubDate>
		<dc:creator>Alderete</dc:creator>
				<category><![CDATA[How To]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[thesiswp]]></category>

		<guid isPermaLink="false">http://aldosoft.com/?p=513</guid>
		<description><![CDATA[As an addition to my article on how to use the [Thesis Custom Loop API](/blog/2011/01/the-thesis-custom-loop-api/), here is a Custom Loop starter template you can use for your own loops. Every loop you can customize in Thesis 1.8 is stubbed in, and comments before each method provide helpful reference information. All you need to do is replace the one-line stub methods with your own custom loop code. You can delete any loop method you're not customizing, or leave them in place for the future; they simply apply the appropriate default Thesis loop.<p><hr />Copyright &copy; 2001-2011 by <a href="http://aldosoft.com">Aldosoft</a>. All rights reserved. This feed is provided for personal non-commercial use only. If you are not reading this material in your news aggregator, the site you are looking at is guilty of copyright infringement. Please contact legal-2011@aldosoft.com so we can take action immediately.</p>
]]></description>
			<content:encoded><![CDATA[<p></p><p>As an addition to my article on <a href="/blog/2011/01/the-thesis-custom-loop-api/">How to Use the Thesis Custom Loop API</a>, here is a <a href="#template">Custom Loop starter template</a> you can use for your own loops. Every loop you can customize in Thesis 1.8 is stubbed in, and comments before each method provide helpful reference information. All you need to do is replace the one-line stub methods with your own custom loop code. You can delete any loop method you&#8217;re not customizing, or leave them in place for the future; they simply apply the appropriate default Thesis loop.</p>

<p><span id="more-513"></span></p>

<p><em>(The comments more than double the file size. If you find them distracting, <a href="/downloads/thesis_custom_loops.zip">here&#8217;s a truly minimal version</a>.)</em></p>

<h3 id="template">Thesis Custom Loop Template</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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #009933; font-style: italic;">/**
 * These are the different Thesis loops that can be customized.
 *
 * Replace the calls to the default Thesis loops with your own code.
 * You can also delete any unused loop methods.
 *
 * @link http://codex.wordpress.org/Conditional_Tags More details of 
 *       which page requests trigger which specific loops.
 * @link http://aldosoft.com/blog/2011/01/the-thesis-custom-loop-api/ Detailed
 *       article about how to use the Custom Loop API.
 */</span>
<span style="color: #000000; font-weight: bold;">class</span> my_looper <span style="color: #000000; font-weight: bold;">extends</span> thesis_custom_loop <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * The loop for the blog index page.
	 *
	 * By default this is the home or top-level index page.
	 * Can be changed to any page using Settings &gt; Reading &gt; Posts Page.
	 *
	 * This loop is used by the {@link archive()} loop (and all child loops)
	 * unless Thesis &gt; Design Options &gt; Display Options &gt; Archives is
	 * set to Titles Only.
	 */</span>
	<span style="color: #000000; font-weight: bold;">function</span> home<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		thesis_loop<span style="color: #339933;">::</span><span style="color: #004000;">home</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * The loop for the front page of the site if configured in 
	 * Settings &gt; Reading &gt; Front Page. 
	 *
	 * Has no effect if no front page is set.
	 *
	 * By default, delegates to the {@link page()} loop.
	 */</span>
	<span style="color: #000000; font-weight: bold;">function</span> front<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		thesis_loop<span style="color: #339933;">::</span><span style="color: #004000;">front</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * The loop for displaying an individual post, i.e. the 
	 * one-article-per-page view of a post.
	 *
	 * Sometimes referred to as &quot;single-entry pages&quot;.
	 */</span>
	<span style="color: #000000; font-weight: bold;">function</span> single<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		thesis_loop<span style="color: #339933;">::</span><span style="color: #004000;">single</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * The loop for displaying an individual attachment on a post, 
	 * usually an image.
	 *
	 * By default, delegates to the {@link single()} loop.
	 */</span>
	<span style="color: #000000; font-weight: bold;">function</span> attachment<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		thesis_loop<span style="color: #339933;">::</span><span style="color: #004000;">attachment</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * The loop for a static, individual page.
	 */</span>
	<span style="color: #000000; font-weight: bold;">function</span> page<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		thesis_loop<span style="color: #339933;">::</span><span style="color: #004000;">page</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Default loop for archive-style pages.
	 *
	 * This loop is the default for category, tag, tax, author,
	 * day, month, year, and search pages.
	 *
	 * Important: Delegates to {@link home()} unless 
	 * Thesis &gt; Design Options &gt; Display Options &gt; Archives is 
	 * set to Titles Only.
	 */</span>
	<span style="color: #000000; font-weight: bold;">function</span> archive<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		thesis_loop<span style="color: #339933;">::</span><span style="color: #004000;">archive</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Loop for category pages.
	 *
	 * By default, delegates to the {@link archive()} loop.
	 */</span>
	<span style="color: #000000; font-weight: bold;">function</span> category<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		thesis_loop<span style="color: #339933;">::</span><span style="color: #004000;">category</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Loop for tag pages.
	 *
	 * By default, delegates to the {@link archive()} loop.
	 */</span>
	<span style="color: #000000; font-weight: bold;">function</span> tag<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		thesis_loop<span style="color: #339933;">::</span><span style="color: #004000;">tag</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Loop for custom taxonomy pages.
	 *
	 * Has no effect unless you have defined a custom taxonomy.
	 *
	 * By default, delegates to the {@link archive()} loop.
	 */</span>
	<span style="color: #000000; font-weight: bold;">function</span> tax<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		thesis_loop<span style="color: #339933;">::</span><span style="color: #004000;">tax</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Loop for author pages.
	 *
	 * By default, delegates to the {@link archive()} loop.
	 */</span>
	<span style="color: #000000; font-weight: bold;">function</span> author<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		thesis_loop<span style="color: #339933;">::</span><span style="color: #004000;">author</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Loop for year-level date archive pages.
	 *
	 * By default, delegates to the {@link archive()} loop.
	 */</span>
	<span style="color: #000000; font-weight: bold;">function</span> year<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		thesis_loop<span style="color: #339933;">::</span><span style="color: #004000;">year</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Loop for month-level date archive pages.
	 *
	 * By default, delegates to the {@link archive()} loop.
	 */</span>
	<span style="color: #000000; font-weight: bold;">function</span> month<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		thesis_loop<span style="color: #339933;">::</span><span style="color: #004000;">month</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Loop for day-level date archive pages.
	 *
	 * By default, delegates to the {@link archive()} loop.
	 */</span>
	<span style="color: #000000; font-weight: bold;">function</span> day<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		thesis_loop<span style="color: #339933;">::</span><span style="color: #004000;">day</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Loop for search results page.
	 *
	 * By default, delegates to the {@link archive()} loop IF THERE ARE RESULTS.
	 * By default, just shows a search form if there are no results.
	 *
	 * A custom search loop needs to deal with both possibilities; see the
	 * {@link thesis_loop::search()} for example of how to do so.
	 */</span>
	<span style="color: #000000; font-weight: bold;">function</span> search<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		thesis_loop<span style="color: #339933;">::</span><span style="color: #004000;">search</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Loop for the &quot;404 Not Found&quot; error page.
	 * 
	 * Runs when the URL requested could not be mapped to any page.
	 */</span>
	<span style="color: #000000; font-weight: bold;">function</span> fourohfour<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		thesis_loop<span style="color: #339933;">::</span><span style="color: #004000;">fourohfour</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Loop for page without any posts/pages (an empty query).
	 *
	 * Runs when the URL requested maps to a legitimate page, but there
	 * is no post data to display. Should be rare.
	 */</span>
	<span style="color: #000000; font-weight: bold;">function</span> nothing<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		thesis_loop<span style="color: #339933;">::</span><span style="color: #004000;">nothing</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">/* PRIVATE HELPER METHODS */</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Merge new query parameters with existing parameters.
	 *
	 * This preserves existing request parameters, for compatibility
	 * with plugins, etc.
	 *
	 * New parameters overwrite existing parameters with same name.
	 *
	 * @access private
	 * @param array $new_params Parameters for a new query via 
	 *        {@link query_posts()} or a new {@link WP_Query}.
	 * @global $wp_query is used to get the current request parameters.
	 *
	 * @return array The merged parameters array, ready for passing to 
	 *               {@link WP_Query::query_posts()} or
	 *               {@link WP_Query::get_posts()}.
	 */</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> preserve_query_params<span style="color: #009900;">&#40;</span><span style="color: #000088;">$new_params</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$wp_query</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">return</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array_merge</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$wp_query</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">query</span><span style="color: #339933;">,</span> <span style="color: #000088;">$new_params</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #009933; font-style: italic;">/**
	 * Retrieve the current &quot;paged&quot; parameter from the request.
	 *
	 * This is used for &quot;paging&quot; through the query results. 
	 * See {@link http://aldosoft.com/blog/2011/01/thesis-custom-loop-template/ 
	 * for details.}
	 *
	 * This method deals with a WordPress idiosyncracy / inconsistency in the 
	 * name of this parameter.
	 *
	 * @access private
	 * @uses WP_Query::get_query_var()
	 *
	 * @return int The current paged parameter.
	 */</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> get_paged_param<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>is_front_page<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$paged</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>get_query_var<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'page'</span><span style="color: #009900;">&#41;</span> ? get_query_var<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'page'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
			<span style="color: #000088;">$paged</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>get_query_var<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'paged'</span><span style="color: #009900;">&#41;</span> ? get_query_var<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'paged'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #b1b100;">return</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$paged</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000088;">$custom_looper</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> my_looper<span style="color: #339933;">;</span></pre></td></tr></table></div>


<h3>About the Helper Methods</h3>

<p>I&#8217;ve added two private methods that virtually every Custom Loop will find a way to make use of. (Lines 197-200 and 217-225.) These helpers simplify your loop code by factoring out dealing with some idiosyncrasies of WordPress request parameters.</p>

<p>The first helper makes preserving existing request parameters easier, and more complete:</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="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> my_looper <span style="color: #000000; font-weight: bold;">extends</span> thesis_custom_loop <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">function</span> home<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$new_args</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
			<span style="color: #0000ff;">'orderby'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'title'</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">'order'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'ASC'</span><span style="color: #339933;">,</span>
		<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		query_posts<span style="color: #009900;">&#40;</span><span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">preserve_query_params</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$new_args</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		thesis_loop<span style="color: #339933;">::</span><span style="color: #004000;">home</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
<span style="color: #000088;">$custom_looper</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> my_looper<span style="color: #339933;">;</span></pre></td></tr></table></div>


<p>This loop is functionally (almost) identical to the <a href="http://aldosoft.com/blog/2011/01/the-thesis-custom-loop-api/#simple-loop">first loop example in the Custom Loop API article</a>. It changes the sort order of the home page, but otherwise leaves the formatting alone. At first blush it&#8217;s just a more complicated version of the earlier example, but let&#8217;s look at what&#8217;s different:</p>

<ul>
<li><p>Lines 4-7 define an array of query arguments, which will override existing request parameters. Recall from the earlier article that using an array is generally easier when you have more than one or two parameters to add.</p></li>
<li><p>Line 8 is what&#8217;s interesting, instead of concatenating together pieces of a query string, I use the new helper method, <code>preserve_query_params()</code>. The cool thing about the helper is that it accesses the <code>$wp_query</code> global to extract <em>all</em> of the request parameters, not merely those that are in the query string. This is more likely to preserve compatibility with plugins, etc.</p></li>
<li><p>Line 9, delegate to the built-in Thesis <code>home()</code> loop, just as I did before.</p></li>
</ul>

<p>The <code>preserve_query_params()</code> method will generally be useful in any Custom Loop where you modify the <em>existing</em> query; it&#8217;s unnecessary in situations where you are creating a <em>new</em> query, unless that query needs to &#8220;copy&#8221; parameters from the default query.</p>

<p>The second helper method, <code>get_paged_param()</code>, is for use when you want to enable paging through the results of a <em>new</em> query, that is, when you are adding a list of items that would not normally display on the URL requested. The <a href="http://aldosoft.com/blog/2011/01/the-thesis-custom-loop-api/#portfolio-loop">portfolio page example in the Custom Loop API article</a> is a good example of where this is useful. Here&#8217;s an abbreviated version for discussion:</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
26
27
28
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> page<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>is_page<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'portfolio'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// Get request var to maintain paging through list of portfolio posts</span>
        <span style="color: #000088;">$paged</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get_paged_param</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Loop #1: Show the requested page's content</span>
        <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span>have_posts<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">// Standard WordPress</span>
            the_post<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Standard WordPress</span>
			<span style="color: #666666; font-style: italic;">// ... HTML output</span>
        <span style="color: #009900;">&#125;</span>
        wp_reset_query<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// Loop #2: Add a list of portfolio posts</span>
        <span style="color: #000088;">$portfolio_query_args</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
            <span style="color: #0000ff;">'paged'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$paged</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">'post_type'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'post'</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">'post_status'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'publish'</span><span style="color: #339933;">,</span>
            <span style="color: #0000ff;">'tag'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'portfolio'</span><span style="color: #339933;">,</span>
        <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        query_posts<span style="color: #009900;">&#40;</span><span style="color: #000088;">$portfolio_query_args</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>have_posts<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span>have_posts<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            the_post<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #666666; font-style: italic;">// ... HTML output</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #666666; font-style: italic;">// else ...</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>


<p>The basic idea is that one page of this site is a portfolio, which will display a stylized list of posts which have been tagged as &#8220;portfolio&#8221;. The page itself contains static content to introduce the portfolio, so we&#8217;re adding a second query to get the portfolio items. The paging issue arises when we have more portfolio posts than will fit in the list (WordPress has a default of 10 here, it can be changed in <span class="ui">Settings > Reading > Blog pages show at most</span>).</p>

<p>If we want to be able to &#8220;page&#8221; backwards and see older portfolio items, we need to enable the &#8220;paging controls&#8221;, the &#8220;Previous Entries&#8221; and &#8220;Next Entries&#8221; links that appear at the bottom of the page in Thesis archive views. These links are auto-generated by Thesis and WordPress functions; all we need to do to enable them is to preserve the &#8220;paged&#8221; parameter that is part of the request, and feed it to the new query.</p>

<p>So, on line 4 we use the <code>get_paged_param()</code> helper method to grab that parameter. The helper deals with an inconsistency in WordPress, where the parameter is named one thing on most pages, and another on the front page.</p>

<p>Lines 7-11 handle the initial Loop, which is simply displaying the static page content. (And, obviously, line 9 hides a lot of the details; see <a href="http://aldosoft.com/blog/2011/01/the-thesis-custom-loop-api/#portfolio-loop">the portfolio example</a> for the full code.)</p>

<p>Line 14 starts our second Loop, and line 15 is where we add the <code>$paged</code> value to the parameters for the new query. This is the query that will return multiple results, potentially more than will display on one page, and which we want to page through. Adding the <code>$paged</code> parameter tells the query which section of the results to actually retrieve.</p>

<p>On line 20 we run the query, and lines 22-25 deal with looping through the results of the query and outputting the portfolio list HTML for each portfolio post.</p>

<p>Now, there&#8217;s additional complexity here that might not be apparent. The <a href="http://codex.wordpress.org/Template_Tags/query_posts">WordPress <code>query_posts()</code> documentation</a> <em>explicitly</em> states that you should only use <code>query_posts()</code> to modify the main page Loop. That&#8217;s a good rule of thumb, and if you choose to not follow it, you should have a good reason.</p>

<p>In this case, the reason is pagination. If the Loop above were to use <code>get_posts()</code> or create a new <code>WP_Query</code> object manually, it would certainly retrieve the portfolio posts just as well as using <code>query_posts()</code>, without making the code appreciably more complicated.</p>

<p>But the automatic display of the paging controls will only happen for the query results <em>last returned by <code>query_posts()</code>.</em> If the above loop did not use <code>query_posts()</code>, the default Thesis pagination wouldn&#8217;t happen, and I would have to write my own pagination code, which would add a <em>lot</em> more code to the loop. This way is easier.</p>

<p>How does it work? WordPress pagination runs off the global <code>$wp_query</code> variable. When you leave your loop (or any hook affecting the &#8220;main&#8221; query), you need to leave <code>$wp_query</code> holding the results you want paginated <em>and</em> with a valid/preserved <code>$paged</code> parameter. If you do that, pagination runs automatically.</p>

<p>Anyway, to at least wrap up a long story, hopefully this starter template and the helper methods will make your Custom Loops easier to write. Happy looping!</p>

<p><hr />Copyright &copy; 2001-2011 by <a href="http://aldosoft.com">Aldosoft</a>. All rights reserved. This feed is provided for personal non-commercial use only. If you are not reading this material in your news aggregator, the site you are looking at is guilty of copyright infringement. Please contact legal-2011@aldosoft.com so we can take action immediately.</p>
]]></content:encoded>
			<wfw:commentRss>http://aldosoft.com/blog/2011/01/thesis-custom-loop-template/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>How to use the Thesis Custom Loop API</title>
		<link>http://aldosoft.com/blog/2011/01/the-thesis-custom-loop-api/</link>
		<comments>http://aldosoft.com/blog/2011/01/the-thesis-custom-loop-api/#comments</comments>
		<pubDate>Tue, 18 Jan 2011 00:00:20 +0000</pubDate>
		<dc:creator>Alderete</dc:creator>
				<category><![CDATA[How To]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[thesiswp]]></category>

		<guid isPermaLink="false">http://aldosoft.com/?p=436</guid>
		<description><![CDATA[In the latest release of the Thesis theme for WordPress a new feature for advanced theme customization was added called the Thesis Custom Loop API. This article explains in detail what the Custom Loop API is, where it's useful, and how to use it.<p><hr />Copyright &copy; 2001-2011 by <a href="http://aldosoft.com">Aldosoft</a>. All rights reserved. This feed is provided for personal non-commercial use only. If you are not reading this material in your news aggregator, the site you are looking at is guilty of copyright infringement. Please contact legal-2011@aldosoft.com so we can take action immediately.</p>
]]></description>
			<content:encoded><![CDATA[<p></p><p>In the latest release of the <a href="/link/thesis-theme">Thesis theme for WordPress</a> a new feature for advanced theme customization was added called the <strong>Thesis Custom Loop API.</strong> The Thesis User&#8217;s Guide explains <a href="http://diythemes.com/thesis/rtfm/custom-loop-api/">the basics of how to use the Custom Loop API</a>. This article expands on that documentation, offering additional context to explain what the Custom Loop API is, what it&#8217;s good for, and examples of how to use it.</p>

<p class="note"><strong>Wait! There&#8217;s more!</strong> I&#8217;ve written a follow-up to this article, <a href="http://aldosoft.com/blog/2011/01/thesis-custom-loop-template/">Thesis Custom Loop Starter Template</a>, which provides a complete custom loop class ready for you to customize, along with useful helper methods and additional explanations.</p>

<h3>A Bit of WordPress and Thesis Background</h3>

<p>To understand what the Thesis Custom Loop is, we need to take a step back, and look at just WordPress. Setting aside plugins for a moment, before the Thesis theme, there was only one way to alter the HTML and contents of your site&#8217;s theme: hack away on the theme files themselves. And once you started hacking on your copy of a theme&#8217;s files, it became a real challenge to update to a newer version of that theme.</p>

<p>Among other innovations, the Thesis theme made it possible to customize the theme extensively <em>without</em> altering the theme&#8217;s files. By isolating theme customizations into a few files in the <code>thesis/custom</code> directory, it became easy, almost trivial to update to newer versions of the theme. (These days child theme functionality is built into WordPress, but it works differently than Thesis, overriding theme files directly, rather than with an API, so it&#8217;s not relevant here.)</p>

<p>Thesis achieved this by moving the vast majority of the theme&#8217;s code out of standard WordPress theme files and into libraries and classes in the Thesis core code, and weaving into it the Thesis API&#8212;the hooks and filters we know and love.</p>

<p>With Thesis, you don&#8217;t hack away on the theme files, because there is nothing there. (Check out the <code>thesis/index.php</code> file, it has <em>one</em> line of code.) Instead of altering the HTML and WordPress Loop code directly, you use the hooks and filters to re-order, remove, or alter the various pieces of the Thesis HTML, post content, metadata about those posts (tags, categories, author, etc.), and so on.</p>

<h3>What is the Thesis Custom Loop API?</h3>

<p>If you have worked with Thesis much, at this point you&#8217;re thinking &#8220;I know all this, but what does it have to do with the Custom Loop API?&#8221;</p>

<p>Standard WordPress theme files don&#8217;t merely contain HTML and content tags, they also contain <strong><a href="http://codex.wordpress.org/The_Loop_in_Action">The Loop</a>.</strong> The Loop is the heart of WordPress output, and it is how WordPress acquires, processes, and iterates through a set of posts (or pages, etc.) to convert them from database table rows to e.g. your blog&#8217;s home page.</p>

<p><em>When Thesis moved the contents of theme files from the files themselves to functions and classes inside Thesis, it also moved The Loop.</em> While you can use hooks and filters to alter the HTML that Thesis generates when processing The Loop, before Thesis 1.8 there was no (good) way to use anything but a standard WordPress Loop on a Thesis site.</p>

<p>The Thesis Custom Loop API opens that up again. A cynic might say it just returns a built-in WordPress feature that was taken away by earlier versions of Thesis, but actually the Thesis Custom Loop can be a much cleaner way to work with The Loop. We&#8217;ll see how in a bit.</p>

<p><span id="more-436"></span></p>

<h3>The Loop Inside the Custom Loop API</h3>

<p>The WordPress Codex has a <a href="http://codex.wordpress.org/The_Loop_in_Action">solid introduction to The Loop</a> and <a href="http://codex.wordpress.org/The_Loop">developer details including multiple examples</a>, which explain what the WordPress Loop is, and why and how a WordPress developer might want to modify it. <a href="http://www.kristarella.com/2010/02/wordpress-custom-loops/">Kristarella.com also has a great take on WordPress Loops</a>. Since the Custom Loop API is simply the Thesis way to affect The Loop, at some point you will want to read these docs carefully, especially to achieve advanced customizations.</p>

<p>To explore the Thesis Custom Loop API we can use a simplified version of <a href="http://codex.wordpress.org/Query_Overview">what happens during a request to a WordPress blog</a> (ignoring initialization, error conditions, and other distractions):</p>

<ol>
<li>Parse the URL requested into a set of query conditions and variable settings.</li>
<li>Run the query against the database.</li>
<li>Turn the database results into a structured list of posts.</li>
<li>Pick out the appropriate theme file (index, category, author, etc.), and turn control over to it.</li>
<li>The theme file runs <a href="http://codex.wordpress.org/The_Loop_in_Action">The Loop</a> to convert the list of posts into HTML.</li>
</ol>

<p>To simplify it even further, you can think of it this way:</p>

<ol>
<li>Get some posts.</li>
<li>Process the posts into HTML. (Technically, only this second part is The Loop.)</li>
</ol>

<p>The HTML generated by The Loop affects each item processed by The Loop, whether that is a collection of posts, e.g. for the blog index or an archive page, a single item, e.g. for a static page or individual post, or (in rare situations) no items at all. All of the HTML generated is placed inside the Thesis #content div, i.e.:</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="font-family:monospace;"><span style="color: #339933;">&lt;!--</span> <span style="color: #009900;">&#91;</span><span style="color: #339933;">...</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">--&gt;</span>
<span style="color: #339933;">&lt;</span>div id<span style="color: #339933;">=</span><span style="color: #0000ff;">&quot;content&quot;</span><span style="color: #339933;">&gt;</span>
    <span style="color: #009900;">&#40;</span>The Loop output goes here<span style="color: #339933;">.</span><span style="color: #009900;">&#41;</span>
<span style="color: #339933;">&lt;/</span>div<span style="color: #339933;">&gt;</span>
<span style="color: #339933;">&lt;!--</span> <span style="color: #009900;">&#91;</span><span style="color: #339933;">...</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">--&gt;</span></pre></td></tr></table></div>


<h3>Why Play with the Loop?</h3>

<p>With all that background under our belts, it shouldn&#8217;t be hard to think of reasons why you might want to alter, extend, or replace the standard WordPress Loop, to create pages which are significantly different from standard WordPress or Thesis versions. The most obvious thing to do is to alter the set of posts that are retrieved from the database. Possibilities include:</p>

<ul>
<li>Alter the sort order of the posts, e.g., to sort teasers alphabetically.</li>
<li>Show posts only from certain categories, e.g., shared link items or featured posts.</li>
<li>Exclude posts from certain categories, e.g., your archived tweets auto-created by <a href="http://crowdfavorite.com/wordpress/plugins/twitter-tools/">Twitter Tools</a>.</li>
<li>Do any of these for specific tags, authors, post custom fields, etc.</li>
</ul>

<p>Another thing you can do by altering The Loop is to affect output, but only when displaying certain pages. For example, you might want to:</p>

<ul>
<li>Start a page (the Home page, a certain Category page, or any specific 
static page) off with some static content, and then display a list of posts.</li>
<li>Add a list of posts to an existing static page.</li>
<li>Treat custom post type or custom taxonomy archive pages differently than &#8220;standard&#8221; pages.</li>
<li>Create a custom Authors page.</li>
</ul>

<p>And then there are times when you want to customize the HTML for a page beyond what&#8217;s available via hooks and filters. That should be rare, but with the Custom Loop API, you can completely replace the HTML output for the #content div of any page.</p>

<h3 id="simple-loop">Getting Started: A Simple Custom Loop</h3>

<p>Here is possibly the simplest Custom Loop that actually does something useful (put this in your <code>thesis/custom/custom_functions.php</code> file):</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="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> tlooper <span style="color: #000000; font-weight: bold;">extends</span> thesis_custom_loop <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">function</span> home<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$query_string</span><span style="color: #339933;">;</span>
		query_posts<span style="color: #009900;">&#40;</span><span style="color: #000088;">$query_string</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&amp;orderby=title&amp;order=ASC&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		thesis_loop<span style="color: #339933;">::</span><span style="color: #004000;">home</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span>
<span style="color: #000088;">$the_looper</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> tlooper<span style="color: #339933;">;</span></pre></td></tr></table></div>


<p>This Custom Loop changes the sort order for the home page, to sort by post title, instead of by date. (Why is this useful? Image a site that lists books or movies, you may not care about when a title was added to the site, you just want to look alphabetically. At any rate, it was <a href="http://diythemes.com/forums/showthread.php?37116-Just-want-the-teasers-to-show-alphabetically">requested in the Thesis Support Forum</a>.)</p>

<p>It also illustrates the fundamentals of creating your own Custom Loop. First, there is the essential structure:</p>

<ol>
<li>Lines 1-9 define a new Custom Loop class, which extends the <code>thesis_custom_loop</code> class. </li>
<li>Within the class, lines 3-7 define a function to be used for the loop to be changed, in this case, the loop for the home page.</li>
<li>Line 10 creates a new instance of this class, which is the &#8220;trigger&#8221; that tells Thesis to actually use it.</li>
</ol>

<p>This structure of: a class that extends <code>thesis_custom_loop</code>; one or more loop functions, and; activation by instantiating the class, is pretty much the whole Custom Loop API. Indeed, the class definition and instantiation are each one line, and they are dead simple. All of the complexity of creating Custom Loops will be in the loop functions&#8212;and they don&#8217;t have to be that complicated.</p>

<p>Let&#8217;s look at this simple loop function again, line-by-line:</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="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> home<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$query_string</span><span style="color: #339933;">;</span>
	query_posts<span style="color: #009900;">&#40;</span><span style="color: #000088;">$query_string</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&amp;orderby=title&amp;order=ASC&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	thesis_loop<span style="color: #339933;">::</span><span style="color: #004000;">home</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>


<ol>
<li><p>The function signature. Two things to note. First, there are no parameters. None of the Custom Loop functions take parameters; any request params or WordPress variables or settings you might want to access must be brought in from the global scope. Second, the method name is one of 16 defined in the Custom Loop API (see <a href="#api-reference">the API Reference section</a> below for specifics). Each one represents a request type (home, post, archives, category, etc.) for which you can override the loop definition. More on this below.</p></li>
<li><p>Bring the variable <code>$query_string</code> into scope. <code>$query_string</code> is a WordPress global variable, which contains the query string portion of the URL requested, i.e., everything after the &#8220;?&#8221; in the URL.</p></li>
<li><p>Run the query for posts, with two new parameters added to the query string. <code>query_posts()</code> is a WordPress function that runs the database query and puts the results in a WordPress global variable for use inside The Loop. When called with parameters, the new parameters override whatever WordPress parsed out of the request URL. Here, appending the new parameters to the existing query string preserves any other query parameters that might already exist.</p></li>
<li><p>Call the default Thesis HTML output loop for the home page (this is basically just a call to the superclass). This turns control back over to Thesis to handle the conversion of database results into HTML.</p></li>
</ol>

<p>To go back to our ultra-simplified explanation of what happens in The Loop&#8230;</p>

<div class="reprise"><ol>
    <li>Get some posts.</li>
    <li>Process the posts into HTML.</li>
</ol></div>

<p>&#8230;this Custom Loop only affects #1, and leaves #2 unchanged. Processing the posts into HTML can be a lot of work, so it&#8217;s <em>really cool</em> that Thesis lets you skip that part, if the default HTML is fine. (Traditional WordPress themes don&#8217;t allow you to skip that part.)</p>

<p>This example is extremely simple, but it&#8217;s a great model for any situation where you just want to change the query that gathers the posts to display on a page. You only have to change <em>one line</em> to change the query. To offer a few variations on this initial theme, here are a few more useful query-only custom loops:</p>

<h4>Order by the number of comments on the post, most comments to least:</h4>


<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="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> home<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$query_string</span><span style="color: #339933;">;</span>
	query_posts<span style="color: #009900;">&#40;</span><span style="color: #000088;">$query_string</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&amp;orderby=comment_count&amp;order=DESC&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	thesis_loop<span style="color: #339933;">::</span><span style="color: #004000;">home</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>


<h4>Show only posts from certain categories:</h4>


<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="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> home<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$query_string</span><span style="color: #339933;">;</span>
	query_posts<span style="color: #009900;">&#40;</span><span style="color: #000088;">$query_string</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&amp;cat=3,4&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	thesis_loop<span style="color: #339933;">::</span><span style="color: #004000;">home</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>


<h4>Exclude posts from certain categories:</h4>


<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="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> home<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$query_string</span><span style="color: #339933;">;</span>
	query_posts<span style="color: #009900;">&#40;</span><span style="color: #000088;">$query_string</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&amp;cat=-1,-14&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	thesis_loop<span style="color: #339933;">::</span><span style="color: #004000;">home</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>


<h4>Show posts by a specific author with a specific tag:</h4>


<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="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> home<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$query_string</span><span style="color: #339933;">;</span>
	query_posts<span style="color: #009900;">&#40;</span><span style="color: #000088;">$query_string</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&amp;author_name=Twain&amp;tag=american&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	thesis_loop<span style="color: #339933;">::</span><span style="color: #004000;">home</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>


<p>See the <a href="http://codex.wordpress.org/Template_Tags/query_posts">WordPress Codex page on <code>query_posts()</code></a> for more documentation on the various parameters you can use to affect the query, there is a <em>lot</em> you can do here.</p>

<h3 id="special-page-loop">A More Complex Loop: Make A Page &#8220;Special&#8221;</h3>

<p>Here&#8217;s the scenario: We have a site where we&#8217;re posting a selection of books from <a href="http://www.gutenberg.org/">Project Gutenberg</a>. Our site has three categories: fiction, non-fiction, and announcements. The first two are for the books, the third is for notifications for site visitors.</p>

<p>So, on the category archive pages for books, we want to list posts in alphabetical order by title. For the announcements, we want a date order. Additionally, we want to format the two different kinds of items, well, differently.</p>

<p>Here&#8217;s a custom <code>category()</code> loop method that handles 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
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> category<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>is_category<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Announcements'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// Default sort order (by date) is correct</span>
		<span style="color: #666666; font-style: italic;">// But we want to change the HTML output</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// This creates standard Thesis HTML content blocks that </span>
		<span style="color: #666666; font-style: italic;">// will automatically pick up Thesis styles</span>
		thesis_archive_intro<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;div class=&quot;post_box top&quot;&gt;'</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'  &lt;div class=&quot;format_text&quot;&gt;'</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'    &lt;ul class=&quot;system_announcements&quot;&gt;'</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// Here's an actual WordPress Loop!</span>
		<span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span>have_posts<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">// This is standard WordPress</span>
			the_post<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Standard WordPress, sets up global variables</span>
&nbsp;
			<span style="color: #666666; font-style: italic;">// Output our notices with custom HTML</span>
			<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'      &lt;li&gt;&lt;strong&gt;&lt;a href=&quot;'</span> <span style="color: #339933;">.</span> get_permalink<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&quot;&gt;'</span> <span style="color: #339933;">.</span> 
			     get_the_title<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">'&lt;/a&gt;&lt;/strong&gt; &amp;mdash; '</span> <span style="color: #339933;">.</span> 
			     get_the_time<span style="color: #009900;">&#40;</span>get_option<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'date_format'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;br /&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span> <span style="color: #339933;">.</span>
			     get_the_content<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;/li&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// This closes the HTML blocks we opened</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;    &lt;/ul&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;  &lt;/div&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;&lt;/div&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">// For all other categories</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// Change the sort order to title, alpha order</span>
		<span style="color: #000000; font-weight: bold;">global</span> <span style="color: #000088;">$query_string</span><span style="color: #339933;">;</span>
		query_posts<span style="color: #009900;">&#40;</span><span style="color: #000088;">$query_string</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&amp;orderby=title&amp;order=ASC&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// But we're happy with the default HTML output, so delegate the rest</span>
		thesis_loop<span style="color: #339933;">::</span><span style="color: #004000;">category</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>


<p>Here are a few interesting points about this example:</p>

<ul>
<li><p>First, this is just the loop method; you need to put this inside a <code>thesis_custom_loop</code> class for it to work. See the first example for the full structure.</p></li>
<li><p>This loop is divided into two sections by the if statement on line 3. The first section (lines 4-30) runs if the requested category is &#8220;Announcements&#8221;, and the second section (the else block on lines 31-39) runs for the other categories. <code>is_category()</code> is a standard WordPress function.</p></li>
<li><p>When the category is Announcements, we want the default WordPress sort order, which is by date, most recent to least recent. So, we don&#8217;t need to modify the query.</p></li>
<li><p>Line 10 uses a Thesis function to output the introduction block for this archive category. This is the headline, and the intro description for the category if you entered one in <span class="ui">Posts &gt; Categories &gt; Introductory Content</span>.</p></li>
<li><p>Lines 11-13 output some basic HTML blocks, using Thesis classes that will pick up styles from Thesis automatically. How do you know what you should output? More on that in a bit.</p></li>
<li><p>Lines 16-17 are the start of The Loop, the standard WordPress version, which we are customizing. Note that it&#8217;s a slightly abbreviated version, we don&#8217;t need the standard <code>if(have_posts())</code> test, because we wouldn&#8217;t be in this loop function if it failed. Thesis &#8220;pre-flights&#8221; the loop, that&#8217;s part of how it decides which Custom Loop function to run on any given request.</p></li>
<li><p>Lines 20-23 are our replacement for the built-in Thesis functions that output a post. Our version is <em>much</em> simpler than what Thesis does in the standard loop. It also doesn&#8217;t run hooks or filters, more on that below.</p></li>
<li><p>Finally, lines 32-38 are the else clause, running for all the other category archive pages. It should look very familiar, from the earlier examples.</p></li>
</ul>

<h3 id="thesis-default-loops">How Do I Know What to Put in a Custom Loop?</h3>

<p>While you may understand all of the code in the previous example, it may be a little mysterious how I came to write it. How did I know what Thesis HTML and classes to use? How did I know to use the <code>thesis_archive_intro()</code> function to start the page? How do you write a Custom Loop that will still invoke hooks and filters?</p>

<p>When working with a traditional WordPress theme, if you want to customize The Loop, you don&#8217;t have to start with a blank slate. The simplest way to get a jumpstart is to make a copy of the relevant theme file, move it into your child theme directory, and modify the code you find in your copied file.</p>

<p>Similarly, the easiest way to make a complex customization of a Thesis loop is to copy the code from the relevant Thesis loop, and paste it into your Custom Loop class. You can find all of the Thesis loops in <code>thesis/lib/classes/loop.php</code>. So, if you want to customize the <code>archive()</code> loop, copy the <code>thesis_loop::archive()</code> method from that file, and paste it into your implementation of <code>thesis_custom_loop</code>. Then start making changes.</p>

<p>Before you do this, though, a word of caution. Thesis is cleverly put together. Before you start hacking away at code you&#8217;ve copied out of <code>loop.php</code> it&#8217;s worth reading and understanding all of the code you find there. You will certainly learn things about how Thesis works, which will better equip you to make changes to those workings. More importantly, it will help you avoid pitfalls, and track down problems you may encounter.<code>&lt;/word&gt;</code></p>

<h3 id="portfolio-loop">Add Posts to a Static Page</h3>

<p>Here&#8217;s the scenario: We have a static page on our site where we would like to add a list of posts with a particular tag below the main page content. For example, perhaps you have a portfolio page that shows off some of your recent projects. Or maybe it&#8217;s a page that archives your tweets. In any event, the goal is to have the content of a static page display, and then have a blog-like index of posts below it, which is <em>not</em> your blog page.</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
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> page<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>is_page<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'portfolio'</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// We need this query var to maintain paging through the list of posts</span>
		<span style="color: #000088;">$paged</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">get_paged_param</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// First, show the page's content</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// This is a stripped down version of the thesis_loop::page() method</span>
		<span style="color: #666666; font-style: italic;">// Post image and comments removed; all hooks preserved</span>
		<span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span>have_posts<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #666666; font-style: italic;">// Standard WordPress</span>
			the_post<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Standard WordPress</span>
&nbsp;
			thesis_hook_before_post_box<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\t</span><span style="color: #000099; font-weight: bold;">\t</span><span style="color: #000099; font-weight: bold;">\t</span>&lt;div class=<span style="color: #000099; font-weight: bold;">\&quot;</span>post_box top<span style="color: #000099; font-weight: bold;">\&quot;</span> id=<span style="color: #000099; font-weight: bold;">\&quot;</span>post-&quot;</span> <span style="color: #339933;">.</span> 
				 get_the_ID<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
			thesis_hook_post_box_top<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			thesis_headline_area<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\t</span><span style="color: #000099; font-weight: bold;">\t</span><span style="color: #000099; font-weight: bold;">\t</span><span style="color: #000099; font-weight: bold;">\t</span>&lt;div class=<span style="color: #000099; font-weight: bold;">\&quot;</span>format_text<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
			thesis_post_content<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\t</span><span style="color: #000099; font-weight: bold;">\t</span><span style="color: #000099; font-weight: bold;">\t</span><span style="color: #000099; font-weight: bold;">\t</span>&lt;/div&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
			thesis_hook_post_box_bottom<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\t</span><span style="color: #000099; font-weight: bold;">\t</span><span style="color: #000099; font-weight: bold;">\t</span>&lt;/div&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
			thesis_hook_after_post_box<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		wp_reset_query<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// Now, show our list of posts (portfolio posts only)</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// Build a set of query arguments, and load in the new set of posts</span>
		<span style="color: #000088;">$portfolio_query_args</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
			<span style="color: #0000ff;">'paged'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$paged</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">'post_type'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'post'</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">'post_status'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'publish'</span><span style="color: #339933;">,</span>
			<span style="color: #0000ff;">'tag'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'portfolio'</span><span style="color: #339933;">,</span>
		<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		query_posts<span style="color: #009900;">&#40;</span><span style="color: #000088;">$portfolio_query_args</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// An initial container div, for CSS styling</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\t</span><span style="color: #000099; font-weight: bold;">\t</span><span style="color: #000099; font-weight: bold;">\t</span>&lt;div class=<span style="color: #000099; font-weight: bold;">\&quot;</span>portfolio<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// Another WordPress loop, to output the portfolio posts</span>
		<span style="color: #666666; font-style: italic;">// Note: this assumes adding proper images to portfolio items</span>
		<span style="color: #666666; font-style: italic;">// And making it look good requires additional CSS styles in custom.css</span>
		<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>have_posts<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> <span style="color: #b1b100;">while</span><span style="color: #009900;">&#40;</span>have_posts<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			the_post<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #000088;">$post_image</span> <span style="color: #339933;">=</span> thesis_post_image_info<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'thumb'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>				
&nbsp;
			<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\t</span><span style="color: #000099; font-weight: bold;">\t</span><span style="color: #000099; font-weight: bold;">\t</span><span style="color: #000099; font-weight: bold;">\t</span>&lt;div class=<span style="color: #000099; font-weight: bold;">\&quot;</span>folio_item post_box<span style="color: #000099; font-weight: bold;">\&quot;</span> id=<span style="color: #000099; font-weight: bold;">\&quot;</span>post-&quot;</span> <span style="color: #339933;">.</span> 
				 get_the_ID<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;&quot;</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\t</span><span style="color: #000099; font-weight: bold;">\t</span><span style="color: #000099; font-weight: bold;">\t</span><span style="color: #000099; font-weight: bold;">\t</span><span style="color: #000099; font-weight: bold;">\t</span>&quot;</span> <span style="color: #339933;">.</span> <span style="color: #000088;">$post_image</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'output'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\t</span><span style="color: #000099; font-weight: bold;">\t</span><span style="color: #000099; font-weight: bold;">\t</span><span style="color: #000099; font-weight: bold;">\t</span><span style="color: #000099; font-weight: bold;">\t</span>&lt;h2&gt;&lt;a href=<span style="color: #000099; font-weight: bold;">\&quot;</span>&quot;</span> <span style="color: #339933;">.</span> get_permalink<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;&quot;</span> <span style="color: #339933;">.</span> 
				 get_the_title<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">.</span> <span style="color: #0000ff;">&quot;&lt;/a&gt;&lt;/h2&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\t</span><span style="color: #000099; font-weight: bold;">\t</span><span style="color: #000099; font-weight: bold;">\t</span><span style="color: #000099; font-weight: bold;">\t</span><span style="color: #000099; font-weight: bold;">\t</span>&lt;div class=<span style="color: #000099; font-weight: bold;">\&quot;</span>format_text<span style="color: #000099; font-weight: bold;">\&quot;</span>&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">echo</span> get_the_excerpt<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\t</span><span style="color: #000099; font-weight: bold;">\t</span><span style="color: #000099; font-weight: bold;">\t</span><span style="color: #000099; font-weight: bold;">\t</span><span style="color: #000099; font-weight: bold;">\t</span>&lt;/div&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
			<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\t</span><span style="color: #000099; font-weight: bold;">\t</span><span style="color: #000099; font-weight: bold;">\t</span><span style="color: #000099; font-weight: bold;">\t</span>&lt;/div&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#125;</span> <span style="color: #666666; font-style: italic;">// end WordPress Loop (both if and while)</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// Close our portfolio container div</span>
		<span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\t</span><span style="color: #000099; font-weight: bold;">\t</span><span style="color: #000099; font-weight: bold;">\t</span>&lt;/div&gt;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
		thesis_loop<span style="color: #339933;">::</span><span style="color: #004000;">page</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">/* PRIVATE METHODS */</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Deal with an irritating inconsistency in WP 3.0.4 and earlier</span>
<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">function</span> get_paged_param<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
	<span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>is_front_page<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$paged</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>get_query_var<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'page'</span><span style="color: #009900;">&#41;</span> ? get_query_var<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'page'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000088;">$paged</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>get_query_var<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'paged'</span><span style="color: #009900;">&#41;</span> ? get_query_var<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'paged'</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">:</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
	<span style="color: #b1b100;">return</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$paged</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>


<p>Understanding this loop:</p>

<ul>
<li><p>First of all, in spite of the length, you should recognize the basic structure. We have a simple &#8220;if this is the special page do something interesting, else just do the normal loop&#8221; structure. If you have a bunch of &#8220;special&#8221; pages on your site, this could get awkward. In that case, create private methods, one for each special page, and call those methods from a case statement in your actual custom loop method.</p></li>
<li><p>The next thing to notice is there are <em>two</em> WordPress Loops! The first one, lines 10-24, is there to display the page&#8217;s content. The second, lines 44-57, is for our list of portfolio posts.</p></li>
<li><p>Line 4 uses a private method to grab a request parameter for what screen of posts to display. That is, if you have more than 10 posts that should display here, preserving this parameter will give you working [Previous] and [Next] links to &#8220;page&#8221; through them.</p>

<p>I put this in a separate method, shown on lines 70-78, because a WordPress inconsistency makes this more complicated than it should be. Also, (I believe) capturing the value needs to be done at the start of the method, before we re-run or reset the query.</p></li>
<li><p>Lines 10-24 are the first Loop, which should only have one item to process, the current static page. The interior of the loop is copy-and-pasted from the Thesis page loop, simplified to remove the Thesis image and WordPress comments elements that don&#8217;t necessarily fit on a portfolio page. All of the Thesis hook functions remain, which means that your custom hook functions will continue to run on this page. If that isn&#8217;t what you want, you can remove them here.</p></li>
<li><p>Line 25 resets the WordPress query object, in preparation for re-using it. This is just basic cleanup, but you need to do it if another query is going to be run on the page.</p></li>
<li><p>Lines 30-35 build up the parameters for our second query. This is a slightly different way of doing it than constructing a query string, as we did in earlier examples. Using an array is generally neater when you have a larger number of parameters, and makes it easier to modify or add to later.</p></li>
<li><p>Line 36 runs the query, putting the items found into the global <code>$wp_query</code> object for use in The Loop.</p></li>
<li><p>Line 39 adds a <code>&lt;div&gt;</code> to the page (line 60 closes it), to contain our portfolio items, and to add a CSS class to allow us to style the portfolio items differently from, say, teasers. (What CSS styling to use is an exercise for the reader, but it goes into your <code>thesis/custom/custom.css</code> file, the same as all of your theme style customizations.)</p></li>
<li><p>Lines 44-57 are the second WordPress Loop. This one is the full monty, because we need to handle the situation where there are no matching posts retrieved by <code>query_posts()</code>. For the most part this just outputs HTML with the relevant Thesis classes attached in the usual places, but does <em>not</em> call hooks or use the Thesis formatting methods, instead using the standard <code>get_the_title()</code> and <code>get_the_excerpt()</code> WordPress functions. Note that we do attach a portfolio-specific class &#8216;folio_item&#8217; to the <code>div</code> containing the portfolio post HTML.</p></li>
<li><p>The one Thesis-specific item in this loop, beyond the CSS classes, is on line 46, where we retrieve the Thesis post image for the item, and then on line 50, where the image HTML is outputted. You can display the thumbnail instead of the full-size image by using <code>thesis_post_image_info('thumb')</code> instead of &#8216;image&#8217;.</p></li>
</ul>

<p>Don&#8217;t let the length of this loop function or any seeming complexity discourage you. I built this loop up slowly, one piece at a time, until I had completed it. Start with a basic if/else or case statement, then add the first WordPress Loop structure with simple output, and then build that output up a line at a time. Then move on to the next piece of your customization. The first one will seem hard, and take a while, but do a couple of loops yourself, and you&#8217;ll be knocking them out like they&#8217;re nothing in no time. Really!</p>

<h3 id="api-reference">Thesis Custom Loop API Reference</h3>

<p>There are a total of 16 different loops you can customize on your Thesis-equipped WordPress site. As explained above, you do this by writing functions inside your Custom Loop class. You can define each of these loop functions inside the same Custom Loop class&#8212;you only need one. (In theory you can create multiple Custom Loop classes, and have them all be active on the same site, but that&#8217;s needlessly complicated.)</p>

<p>Some of the 16 loops available through the Custom Loop API have a hierarchical relationship. That is, by default or with certain settings many of the loops delegate to a &#8220;parent&#8221; loop, and if you customize that parent loop, all of the child loops will be affected as well. In general, you want to customize the <em>most specific</em> loop that you can.</p>

<p>Additionally, each of the 16 loops has a method that delegates control to the corresponding standard Thesis loop, as described in the first loop example above. The method name is the same as the Custom Loop method name, in the form of <code>thesis_loop::method_name()</code>.</p>

<h4>Special Loops</h4>

<p>These loops are unique in some way, mostly by not belonging to a group of related loops.</p>

<ul>
<li><p><strong><code>home()</code></strong></p>

<p>This is the loop for the site&#8217;s blog index page. In a default WordPress configuration, this is the site&#8217;s home or top-level index page. However, this can be changed to any static page, using <span class="ui">Settings &gt; Reading &gt; Posts Page</span>.</p>

<p>Additionally, this loop is used by the archives loop (and all child loops) unless <span class="ui">Thesis &gt; Design Options &gt; Display Options &gt; Archives</span> is set to <span class="ui">Titles Only</span>. If you customize this loop, you may need to also (un)customize <code>$this-&gt;archive()</code>.</p></li>
<li><p><strong><code>search()</code></strong></p>

<p>The loop that handles a search results page. By default it delegates displaying results to the <code>$this-&gt;archive()</code> loop <em>if there are results</em>, but merely shows a search form if there are no results. A custom search loop must handle both conditions.</p></li>
<li><p><strong><code>fourohfour()</code></strong></p>

<p>The loop that handles the &#8220;404 Not Found&#8221; error page. This means that the URL requested does not correspond to any page.</p>

<p>If you want to do something really clever when people try to reach a funky URL, this is the place, but if you just want to display a customized message, a search form, and maybe some popular posts, it&#8217;s probably easier to do it via the 404 hooks.</p></li>
<li><p><strong><code>nothing()</code></strong></p>

<p>The loop that handles a page without any posts or pages. Different from the 404 page, it means that the requested URL maps to a legitimate page, but there is no post data to display. I <em>think</em> this should be rare.</p></li>
</ul>

<h4>Post Loops</h4>

<p>These two loops handle individual posts. Calling this a &#8220;loop&#8221; is somewhat misleading, because in this context the query should only return one post to process.</p>

<ul>
<li><p><strong><code>single()</code></strong></p>

<p>The loop for displaying an individual post, i.e. the one-article-per-page view of a post.</p></li>
<li><p><strong><code>attachment()</code></strong> &#8212; <em>delegates to <code>$this-&gt;single()</code></em></p>

<p>The loop for displaying an individual attachment, usually an image, on a post.</p></li>
</ul>

<h4>Page Loops</h4>

<p>These two loops handle individual static pages. Again, calling this a &#8220;loop&#8221; is somewhat misleading, because in this context the query should only return one page to process.</p>

<ul>
<li><p><strong><code>page()</code></strong></p>

<p>The loop for static pages.</p></li>
<li><p><strong><code>front()</code></strong> &#8212; <em>delegates to <code>$this-&gt;page()</code></em></p>

<p>The loop for a static front page configured in <span class="ui">Settings &gt; Reading &gt; Front Page</span>. Has no effect if no static front page is set.</p></li>
</ul>

<h4>Archive Loops</h4>

<p>These loops handle archive-style pages. By default, all of these use the main <code>archive()</code> loop, which is not to be confused with a static archives page, as described in the Thesis User&#8217;s Guide article <a href="http://diythemes.com/thesis/rtfm/create-customize-archives-page/">&#8220;Create and Customize an Archives Page&#8221;</a>. In this case, the term &#8220;archive&#8221; is simply used generically.</p>

<ul>
<li><p><strong><code>archive()</code></strong></p>

<p>The default loop for all of the archive-style pages. Many Thesis display settings affect the formatting of archives pages, so be sure to start there, before resorting to a custom loop.</p>

<p><strong>Important:</strong> The <code>archive()</code> loop delegates to <code>$this->home()</code> unless <span class="ui">Thesis &gt; Design Options &gt; Display Options &gt; Archives</span> is set to <span class="ui">Titles Only</span>.</p></li>
<li><p><strong><code>category()</code></strong> &#8212; <em>delegates to <code>$this-&gt;archive()</code></em></p>

<p>The loop for category pages, that is, the page you see when you click a category link.</p></li>
<li><p><strong><code>tag()</code></strong> &#8212; <em>delegates to <code>$this-&gt;archive()</code></em></p>

<p>The loop for tag pages. You probably guessed that.</p></li>
<li><p><strong><code>tax()</code></strong> &#8212; <em>delegates to <code>$this-&gt;archive()</code></em></p>

<p>The loop for custom taxonomy pages. Only useful if you have defined a custom taxonomy.</p></li>
<li><p><strong><code>author()</code></strong> &#8212; <em>delegates to <code>$this-&gt;archive()</code></em></p>

<p>The loop handling the display of a single author&#8217;s archive page.</p></li>
<li><p><strong><code>year()</code></strong> &#8212; <em>delegates to <code>$this-&gt;archive()</code></em></p></li>
<li><strong><code>month()</code></strong> &#8212; <em>delegates to <code>$this-&gt;archive()</code></em></li>
<li><p><strong><code>day()</code></strong> &#8212; <em>delegates to <code>$this-&gt;archive()</code></em></p>

<p>I really think you can figure these last three out without my spelling it out for you. :-)</p></li>
</ul>

<p class="note"><strong>Wait! There&#8217;s even more!</strong> I&#8217;ve written a follow-up to this article, <a href="http://aldosoft.com/blog/2011/01/thesis-custom-loop-template/">Thesis Custom Loop Starter Template</a>, which provides a complete, functionally benign custom loop class, along with useful helper methods and additional explanations.</p>

<h3>What <em>Not</em> to Use the Custom Loop API For</h3>

<p>Thesis users got by without the Custom Loop API for quite some time, it was only added in the most recent version of Thesis. If affecting The Loop is so useful, how did people get along without this feature?</p>

<p>The answer is, many effects can be achieved using other features of Thesis, and indeed, before you begin writing a Custom Loop, it&#8217;s always worth investigating whether you can achieve the same end via a setting, or using Thesis hooks and filters. Customizations implemented with those mechanisms will require less maintenance in the long run, as they are more isolated from changes in WordPress or Thesis.</p>

<p>Here are a few things that people often want to do, where you might think you need to create a Custom Loop, along with links to articles or forum posts that show an easier way.</p>

<ul>
<li>Ads, or images, or anything, inserted in-between <em>X</em> number of posts. See <a href="http://diythemes.com/thesis/rtfm/insert-ad-after-content/">Insert Ad After Content</a> and <a href="http://diythemes.com/forums/showthread.php?8517-How-to-add-insert-content-between-teasers-rows&#038;highlight=insert+ad+between+teaser">this forum post</a> for a few ideas.</li>
<li>Add a custom headline or introduction to category pages. In Thesis 1.8, <a href="http://diythemes.com/thesis/version-18-features/#term-options">this is a built-in feature</a>.</li>
<li>A top-level Archives page &#8212; see the Thesis User&#8217;s Guide article <a href="http://diythemes.com/thesis/rtfm/create-customize-archives-page/">&#8220;Create and Customize an Archives Page&#8221;</a> instead.</li>
</ul>

<p>If you&#8217;re a Thesis developer, there are a lot of great articles and forum posts, so it&#8217;s always worth looking for answers before you start digging on your own. Start by reading the various articles about Thesis hooks, and then move onto specific techniques and solutions. Thesis is <em>incredibly</em> flexible, once you get to know it.</p>

<h3>Credits, Resources, and Further Reading</h3>

<p>I found the following articles useful while learning about the Thesis Custom Loop API, and some of my examples are inspired by code I found in them. If you really want to learn more about writing custom loops, these are great places to start.</p>

<ul>
<li><a href="http://diythemes.com/thesis/rtfm/custom-loop-api/">Thesis User&#8217;s Guide: Thesis Custom Loop API</a></li>
<li><a href="http://diythemes.com/forums/showthread.php?36608">Tutorial: Static Front page with paginated posts (Loop API)</a></li>
<li><a href="http://aldosoft.com/blog/2011/01/thesis-custom-loop-template/">Thesis Custom Loop Starter Template</a></li>
<li><a href="http://www.kristarella.com/2010/02/wordpress-custom-loops/">Rockin’ out WordPress custom loops</a></li>
<li><a href="http://codex.wordpress.org/Query_Overview">WordPress Codex: Query Overview</a></li>
<li><a href="http://codex.wordpress.org/Custom_Queries">WordPress Codex: Custom Queries</a></li>
<li><a href="http://codex.wordpress.org/Template_Tags/query_posts">WordPress Codex: query_posts function reference</a></li>
<li><a href="http://codex.wordpress.org/The_Loop_in_Action">WordPress Codex: The Loop in Action</a> (overview)</li>
<li><a href="http://codex.wordpress.org/The_Loop">WordPress Codex: The Loop</a> (details and examples)</li>
<li><a href="http://codex.wordpress.org/Conditional_Tags">WordPress Codex: Conditional Tags</a></li>
</ul>

<p class="note">The code presented here was developed and tested on a standard install of WordPress 3.0.4 with the Thesis Theme for WordPress 1.8. No plugins were activated.</p>

<p><hr />Copyright &copy; 2001-2011 by <a href="http://aldosoft.com">Aldosoft</a>. All rights reserved. This feed is provided for personal non-commercial use only. If you are not reading this material in your news aggregator, the site you are looking at is guilty of copyright infringement. Please contact legal-2011@aldosoft.com so we can take action immediately.</p>
]]></content:encoded>
			<wfw:commentRss>http://aldosoft.com/blog/2011/01/the-thesis-custom-loop-api/feed/</wfw:commentRss>
		<slash:comments>16</slash:comments>
		</item>
		<item>
		<title>Using Typekit with the Thesis Theme for WordPress</title>
		<link>http://aldosoft.com/blog/2009/12/using-typekit-with-thesis-theme-for-wordpress/</link>
		<comments>http://aldosoft.com/blog/2009/12/using-typekit-with-thesis-theme-for-wordpress/#comments</comments>
		<pubDate>Thu, 03 Dec 2009 05:38:59 +0000</pubDate>
		<dc:creator>Alderete</dc:creator>
				<category><![CDATA[How To]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[fonts]]></category>
		<category><![CDATA[thesiswp]]></category>
		<category><![CDATA[typeface]]></category>
		<category><![CDATA[typekit]]></category>

		<guid isPermaLink="false">http://aldosoft.com/?p=377</guid>
		<description><![CDATA[The [Thesis theme for WordPress](/link/thesis-theme) offers excellent typography out of the box. But, like anything else on the web, it is limited in the number of fonts that it can reliably support, because there are not very many "web safe" fonts out there. [TypeKit](http://typekit.com/) is a new service that promises to change that. This document explains how to use TypeKit with Thesis to enable more interesting typography on your Thesis-powered [WordPress](http://wordpress.org/) web site.<p><hr />Copyright &copy; 2001-2011 by <a href="http://aldosoft.com">Aldosoft</a>. All rights reserved. This feed is provided for personal non-commercial use only. If you are not reading this material in your news aggregator, the site you are looking at is guilty of copyright infringement. Please contact legal-2011@aldosoft.com so we can take action immediately.</p>
]]></description>
			<content:encoded><![CDATA[<p></p><p>One of the things that drew me to evaluating and purchasing the <a href="/link/thesis-theme">Thesis theme for WordPress</a> was the outstanding typography that seemed to be a part of every Thesis site I looked at.</p>

<p><img src="http://aldosoft.com/wordpress-content/uploads/2009/12/thesis-font-selection.png" alt="Thesis Font Selection" title="Thesis font options are simple to use" width="299" height="225" class="size-full alignright frame wp-image-387" /> The settings for fonts are simple to use &#8212; you choose a single typeface for each option &#8212; but behind the scenes, Thesis builds a complete &#8220;font stack,&#8221; that is, a comprehensive collection of fonts to use, sorted in order of preference. This ensures that your text will look good, no matter what fonts are installed on a viewer&#8217;s computer.</p>

<p>For example, when you choose <strong>Georgia</strong> for your main font, behind the scenes Thesis specifies a series of fonts that are similar to Georgia:</p>

<p><code>font-family: Georgia, 'Times New Roman', Times, serif;</code></p>

<p>The variety of typefaces offered by Thesis is somewhat limited, due to the nature of fonts on the web today; there&#8217;s only a dozen or so that are &#8220;safe&#8221; to use, because everyone has them. But recently a few services have come along, which take advantage of new features in modern web browsers to offer more variety in type. Possibly the most &#8220;mature&#8221; of these (with mature being a relative term) is <a href="http://typekit.com/">Typekit</a>. But how do you use Typekit with Thesis, when Thesis only allows you to choose fonts from a pre-defined list?</p>

<p>It turns out that this is quite easy to accomplish. In a nutshell, you treat Typekit as a second <code>custom.css</code> file. There are three simple steps:</p>

<p><span id="more-377"></span></p>

<ol>
<li><p>On Typekit, create your &#8220;kit&#8221; for your web site. Use the same CSS selectors here that you would use in your <code>custom.css</code> file if you were specifying them there:</p>

<p><a href="/wordpress-content/uploads/2009/12/typekit-create-kit-with-thesis-selectors.png"><img src="/wordpress-content/uploads/2009/12/typekit-create-kit-with-thesis-selectors-300x265.png" alt="Add Thesis CSS selectors" title="In Typekit, create your font &quot;kit&quot; with Thesis custom CSS selectors. (click for larger version)" width="300" height="265" class="size-medium frame wp-image-381" /></a></p>

<p>You can uncheck any font weights that you&#8217;re not planning to use, and you can leave the default CSS Stack setting. Be sure to click the (big ass?) Publish button!</p></li>
<li><p>When you&#8217;ve got your kit specified, copy the embedding code from Typekit:</p>

<p><a href="/wordpress-content/uploads/2009/12/typekit-copy-javascript.png"><img src="/wordpress-content/uploads/2009/12/typekit-copy-javascript-300x265.png" alt="Copy JavaScript embedding code from Typekit" title="In Typekit, click the Embedding Code link to show the JavaScript you need to copy and paste into Thesis. (click for larger version)" width="300" height="265" class="size-medium frame wp-image-380" /></a></p>

<p>Click the <strong>Embed Code</strong> link at the top right of the page to see this.</p></li>
<li><p>Switch back to your site, and paste the Typekit JavaScript into Thesis, in the Header Scripts section of the Thesis Options panel:</p>

<p><a href="/wordpress-content/uploads/2009/12/typekit-paste-javascript-into-thesis.png"><img src="/wordpress-content/uploads/2009/12/typekit-paste-javascript-into-thesis-293x300.png" alt="Paste Typekit JavaScript into Thesis" title="On your site, on the Thesis Options page, paste the Typekit JavaScript into the Header Scripts field. (click for larger version)" width="293" height="300" class="size-medium frame wp-image-382" /></a></p></li>
</ol>

<p>At this point, you should be able to reload your site, and see the new font(s) in action, wherever you have set them to be used. If you want have additional control over the use of your Typekit fonts, such as bold, italic, text spacing (tracking), etc., you can add it to your <code>custom.css</code> file as normal. This is all straightforward CSS. Typekit is only specifying the font family; all other font settings are handled by Thesis (automatically) or your <code>custom.css</code> file.</p>

<p>There is one aspect of customizing your CSS font specifications that might not be obvious: when a typeface comes with a more broad range of weights, say, from light to ultra bold, how do you specify which weight to use? The answer is very simple, just add a font-weight CSS attribute with a numeric value from this list:</p>


<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">font-weight</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">200</span><span style="color: #00AA00;">;</span> <span style="color: #808080; font-style: italic;">/*  Thin               */</span>
<span style="color: #000000; font-weight: bold;">font-weight</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">300</span><span style="color: #00AA00;">;</span> <span style="color: #808080; font-style: italic;">/*  Light              */</span>
<span style="color: #000000; font-weight: bold;">font-weight</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">400</span><span style="color: #00AA00;">;</span> <span style="color: #808080; font-style: italic;">/*  Normal             */</span>
<span style="color: #000000; font-weight: bold;">font-weight</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">500</span><span style="color: #00AA00;">;</span> <span style="color: #808080; font-style: italic;">/*  Medium / Semi-Bold */</span>
<span style="color: #000000; font-weight: bold;">font-weight</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">600</span><span style="color: #00AA00;">;</span> <span style="color: #808080; font-style: italic;">/* (not used)          */</span>
<span style="color: #000000; font-weight: bold;">font-weight</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">700</span><span style="color: #00AA00;">;</span> <span style="color: #808080; font-style: italic;">/*  Bold               */</span>
<span style="color: #000000; font-weight: bold;">font-weight</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">800</span><span style="color: #00AA00;">;</span> <span style="color: #808080; font-style: italic;">/*  Black              */</span>
<span style="color: #000000; font-weight: bold;">font-weight</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">900</span><span style="color: #00AA00;">;</span> <span style="color: #808080; font-style: italic;">/*  Heavy              */</span></pre></div></div>


<p>So, if you want to use the <a href="http://typekit.com/fonts/136">Milibus typeface</a> for your navigation menu, but the default weight of normal seems a little light to you, while bold is just <em>too</em> bold, to use the semi-bold face just add this to your <code>custom.css</code>:</p>


<div class="wp_syntax"><div class="code"><pre class="css" style="font-family:monospace;"><span style="color: #808080; font-style: italic;">/* Make the weight of the nav menu a little stronger for Milibus */</span>
<span style="color: #6666ff;">.custom</span> ul<span style="color: #6666ff;">.menu</span> li<span style="color: #6666ff;">.tab</span> a <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">font-weight</span><span style="color: #00AA00;">:</span> <span style="color: #cc66cc;">500</span><span style="color: #00AA00;">;</span>  <span style="color: #808080; font-style: italic;">/* semi-bold */</span>
<span style="color: #00AA00;">&#125;</span></pre></div></div>


<p>Again, do not specify the font-family in your <code>custom.css</code> file for elements that will use fonts from Typekit. Typekit handles the font-family for you; you do everything else in <code>custom.css</code>.</p>

<p>I hope this inspires you to get (more) creative with type on your web site!</p>

<p><hr />Copyright &copy; 2001-2011 by <a href="http://aldosoft.com">Aldosoft</a>. All rights reserved. This feed is provided for personal non-commercial use only. If you are not reading this material in your news aggregator, the site you are looking at is guilty of copyright infringement. Please contact legal-2011@aldosoft.com so we can take action immediately.</p>
]]></content:encoded>
			<wfw:commentRss>http://aldosoft.com/blog/2009/12/using-typekit-with-thesis-theme-for-wordpress/feed/</wfw:commentRss>
		<slash:comments>11</slash:comments>
		</item>
		<item>
		<title>Applying Thesis styles in print</title>
		<link>http://aldosoft.com/blog/2009/11/applying-thesis-styles-in-print/</link>
		<comments>http://aldosoft.com/blog/2009/11/applying-thesis-styles-in-print/#comments</comments>
		<pubDate>Thu, 12 Nov 2009 07:31:29 +0000</pubDate>
		<dc:creator>Alderete</dc:creator>
				<category><![CDATA[How To]]></category>
		<category><![CDATA[WordPress]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[printing]]></category>
		<category><![CDATA[stylesheets]]></category>
		<category><![CDATA[thesiswp]]></category>

		<guid isPermaLink="false">http://aldosoft.com/?p=270</guid>
		<description><![CDATA[Although Thesis is the most advanced WordPress theme available, it does not provide a print stylesheet, and so web pages print completely unstyled. This is a problem that is easily solved with Thesis's customizability, and a few lines of CSS.<p><hr />Copyright &copy; 2001-2011 by <a href="http://aldosoft.com">Aldosoft</a>. All rights reserved. This feed is provided for personal non-commercial use only. If you are not reading this material in your news aggregator, the site you are looking at is guilty of copyright infringement. Please contact legal-2011@aldosoft.com so we can take action immediately.</p>
]]></description>
			<content:encoded><![CDATA[<p></p><p>The <a href="/link/thesis-theme">Thesis theme for WordPress</a> is arguably the most advanced premium theme for <a href="http://wordpress.org">WordPress</a>-driven web sites. It combines fantastic layout flexibility (1-, 2-, and 3-columns, in various arrangements) with strong design customizability (colors for virtually every element are customizable, plus borders, padding, fonts and sizing, etc.) and terrific typography. Out of the box it looks great in all major web browsers, and can be customized wildly to achieve <a href="http://diythemes.com/thesis/showcase/?a_aid=4adfe571ce91b">incredible diversity in visual design</a>.</p>

<p>But, oddly, printing pages from a Thesis-based site will often remind you of how the web looked in 1996. That&#8217;s because by default the Thesis theme applies its styling only to on-screen display. Printing is entirely unstyled:</p>

<p><img src="http://aldosoft.com/wordpress-content/uploads/2009/11/thesis_printing-thm1-580x388.png" alt="Thesis on-screen vs. printed" title="Thesis web sites often print completely unstyled, which harkens back to the mid-1990s." width="580" height="388" class="size-large wp-image-294" /></p>

<p>The gory details of the how and why are in a DIYThemes forum posting I made a couple days ago, <a href="http://diythemes.com/forums/customization/16156-printing-like-looks-screen.html#post80030">Thesis prints unstyled by default. This is not good.</a> Fortunately, it is possible to extend Thesis with PHP code, in ways that most themes would never dream of, and this gives us options for better printing.</p>

<p>The beginning of a solution is to add a stylesheet for print; the recommended placement is in <code>thesis/custom/print.css</code>. Then you add the link tag to your site via a hook function in your <code>custom_functions.php</code> file:</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> add_print_stylesheet<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">'&lt;link rel=&quot;stylesheet&quot; href=&quot;'</span> <span style="color: #339933;">.</span> THESIS_CUSTOM_FOLDER <span style="color: #339933;">.</span> 
         <span style="color: #0000ff;">'/print.css&quot; type=&quot;text/css&quot; media=&quot;print&quot; /&gt;'</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">echo</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
add_action<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'wp_head'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'add_print_stylesheet'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>


<p>Now you have an active, but empty, print.css stylesheet. You still have unstyled content when you print. What&#8217;s next?</p>

<p>There are two approaches you can take:</p>

<ul>
<li>Create a new print.css stylesheet from scratch, adding only the minimum styling required to make printouts look the way you want. Ultimately, this can produce the best results.</li>
<li>Import the on-screen stylesheets into the print stylesheet, and then add further additions, customizations, and overrides from there.</li>
</ul>

<p>The second option will result in printouts looking exactly like your site does in a web browser. For some kinds of content this will not be ideal, but for most pages it&#8217;s better than being completely unstyled when printed, and it is a <em>lot</em> easier than writing a new stylesheet from scratch. If you&#8217;re looking for the quickest way to get to &#8220;pretty good,&#8221; this is the way to go.</p>

<p>To do it, put this in your print.css file:</p>


<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="css" style="font-family:monospace;"><span style="color: #a1a100;">@import url(&quot;../style.css&quot;) print;</span>
<span style="color: #a1a100;">@import url(&quot;layout.css&quot;) print;</span>
<span style="color: #a1a100;">@import url(&quot;custom.css&quot;) print;</span>
&nbsp;
<span style="color: #cc00cc;">#sidebars</span> <span style="color: #00AA00;">&#123;</span>
	<span style="color: #000000; font-weight: bold;">display</span><span style="color: #00AA00;">:</span> <span style="color: #993333;">none</span><span style="color: #00AA00;">;</span>
<span style="color: #00AA00;">&#125;</span></pre></td></tr></table></div>


<p>The <code>#sidebars</code> block removes your sidebars when printing. This is what most sites will want to do, but if you don&#8217;t, just delete lines 5-7.</p>

<p><hr />Copyright &copy; 2001-2011 by <a href="http://aldosoft.com">Aldosoft</a>. All rights reserved. This feed is provided for personal non-commercial use only. If you are not reading this material in your news aggregator, the site you are looking at is guilty of copyright infringement. Please contact legal-2011@aldosoft.com so we can take action immediately.</p>
]]></content:encoded>
			<wfw:commentRss>http://aldosoft.com/blog/2009/11/applying-thesis-styles-in-print/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Making Your WordPress Blog Appear in a Non-Root Folder</title>
		<link>http://aldosoft.com/blog/2009/08/blog-in-non-root-folder/</link>
		<comments>http://aldosoft.com/blog/2009/08/blog-in-non-root-folder/#comments</comments>
		<pubDate>Sun, 09 Aug 2009 06:49:21 +0000</pubDate>
		<dc:creator>Alderete</dc:creator>
				<category><![CDATA[How To]]></category>
		<category><![CDATA[WordPress]]></category>

		<guid isPermaLink="false">http://www.aldosoft.dev/?p=38</guid>
		<description><![CDATA[It's easy to use WordPress to build an entirely static content site, based on [Pages](http://codex.wordpress.org/Pages). The setting for changing the Home page from a list of Posts to a static Page is easy to find, and obvious in use. But moving the blog to the same level as other sections of the site is not so obvious. There are multiple related settings, and even the [WordPress Codex page on the topic](http://codex.wordpress.org/Making_Your_Blog_Appear_in_a_Non-Root_Folder) is out-of-date.

This article documents the simplest approach required to build a site like this one, a basic mostly-static WordPress site, where the blog is not the focus of the home page, but pushed down to the same secondary "non-root" level as all the other pages.<p><hr />Copyright &copy; 2001-2011 by <a href="http://aldosoft.com">Aldosoft</a>. All rights reserved. This feed is provided for personal non-commercial use only. If you are not reading this material in your news aggregator, the site you are looking at is guilty of copyright infringement. Please contact legal-2011@aldosoft.com so we can take action immediately.</p>
]]></description>
			<content:encoded><![CDATA[<p></p><p>The Aldosoft site is a mostly static content site, with a few pages describing who we are and what we do. When I started working on the redesign for the site, one of the main things I wanted to add was a blog for, well, articles like this one.</p>

<p>It&#8217;s pretty easy to use <a href="http://wordpress.org/">WordPress</a> to build an entirely static content site, based on <a href="http://codex.wordpress.org/Pages">Pages</a>. This is what the old Aldosoft site did. The setting for changing the Home page from a list of Posts to a static Page is easy to find, and obvious in use.</p>

<p>Putting a blog onto the site, &#8220;pushed off&#8221; the home page to a section at the same level as other pages of the site, is not so obvious. There&#8217;s a number of different settings which seem relevant, but how they all fit together is not clear. <a href="http://codex.wordpress.org/Making_Your_Blog_Appear_in_a_Non-Root_Folder">The documentation in the WordPress Codex</a> (from which the title of this post is taken) is not written with the latest version of WordPress in mind, and makes it seem more complicated than it actually is.</p>

<p>Various Google searches turned up more semi-obsolete, incomplete, or just plain wrong answers. (Any article that says you need to add new files to your theme is outdated, at least for WordPress 2.8+ installations.) This article documents the simplest approach required to build a site like this one, a basic mostly-static WordPress site, where the blog is not the focus of the home page, but pushed down to the same secondary level as all the other pages.</p>

<p><span id="more-38"></span></p>

<p>You need to do three simple things:</p>

<ol>
<li><p>Create a new page (not post), with no content (the page content will be replaced by your blog posts, once you finish step #2). This should be named whatever you want to call the blog section; here it&#8217;s just &#8220;Blog&#8221;. The page slug should be what you want to use for the URL to your blog; in my case it&#8217;s &#8220;blog&#8221;. (Told you this would be simple.)</p></li>
<li><p>Set the front page to &#8220;static&#8221; mode, and activate displaying your posts (your blog) on the new page. Settings > Reading > Front page displays:</p>

<p><img src="http://aldosoft.com/wordpress-content/uploads/2009/08/wp-settings-front-page.png" alt="WordPress Settings &gt; Reading &gt; Front Page" title="Moving your blog off the front page, to a &quot;secondary&quot; level, is very easy." width="431" height="189" class="size-full frame wp-image-182" /></p>

<p>You are setting the &#8220;Front page&#8221; (or root of your site) to be the static page that will be the home page, and the &#8220;Posts page&#8221; to be the page you created in step #1.</p></li>
<li><p>Change your permalink structure, and use the Custom permalink structure to add your blog&#8217;s slug to the permalinks.</p>

<p><img src="http://aldosoft.com/wordpress-content/uploads/2009/08/wp-settings-permalinks-blog.png" alt="WordPress Settings &gt; Permalinks" title="This is how you set your permalink structure to match the same URL for your blog main page." width="550" height="45" class="size-full frame wp-image-183" /></p>

<p>This is optional if your preferred URLs don&#8217;t need to have posts appear &#8220;under&#8221; the main blog listing page. It will work either way.</p></li>
</ol>

<p>There additional steps you will want to take if your theme does not automatically recognize the new Blog page, if you want to add links to your blog archives, etc. But those are steps you would need to take for any WordPress site, not specific to hosting your site&#8217;s blog at the secondary level.</p>

<p><hr />Copyright &copy; 2001-2011 by <a href="http://aldosoft.com">Aldosoft</a>. All rights reserved. This feed is provided for personal non-commercial use only. If you are not reading this material in your news aggregator, the site you are looking at is guilty of copyright infringement. Please contact legal-2011@aldosoft.com so we can take action immediately.</p>
]]></content:encoded>
			<wfw:commentRss>http://aldosoft.com/blog/2009/08/blog-in-non-root-folder/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Installing PostgreSQL server and Ruby gem on Mac OS X</title>
		<link>http://aldosoft.com/blog/2009/04/installing-postgres-for-ruby-on-mac-os-x/</link>
		<comments>http://aldosoft.com/blog/2009/04/installing-postgres-for-ruby-on-mac-os-x/#comments</comments>
		<pubDate>Thu, 23 Apr 2009 01:19:52 +0000</pubDate>
		<dc:creator>Alderete</dc:creator>
				<category><![CDATA[How To]]></category>
		<category><![CDATA[Mac OS X]]></category>
		<category><![CDATA[Ruby on Rails]]></category>
		<category><![CDATA[os-x]]></category>
		<category><![CDATA[postgres]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[ruby]]></category>

		<guid isPermaLink="false">http://www.aldosoft.dev/?p=27</guid>
		<description><![CDATA[How to compile and install both the PostgreSQL relational database server and the associated Ruby database driver for Postgres, for doing Ruby on Rails development with PostgreSQL on Mac OS X 10.5 "Leopard".<p><hr />Copyright &copy; 2001-2011 by <a href="http://aldosoft.com">Aldosoft</a>. All rights reserved. This feed is provided for personal non-commercial use only. If you are not reading this material in your news aggregator, the site you are looking at is guilty of copyright infringement. Please contact legal-2011@aldosoft.com so we can take action immediately.</p>
]]></description>
			<content:encoded><![CDATA[<p></p>	<p>I recently needed to install the PostgreSQL database server on my Mac for development with Ruby on Rails. I ran into a few gotchas, and while I was eventually able to Google and guess my way to success, it took a while. This installation guide is as much for me, 6 months from now, as it is for others who may wish to also install Postgres.</p>

	<p>These instructions were current as of April 22, 2009, and were executed on Mac OS X 10.5.6 with Rubygems 1.3.2 and the Postgres 8.3.7 distribution. The shelf life of these instructions is probably brief, though they should work at least as a rough guide until the next major release of Mac OS X, or of Postgres.</p>

	<p><span id="more-27"></span></p>

	<h2>Preferences and Biases</h2>

	<p>There are other Postgres + Rails instructions which recommend the use of either MacPorts or Fink. I personally dislike these solutions; part of it is having used Fink in its early days and having it screw up royally, and part of it is a sense that the efforts are misplaced. (Why not make your Open Source contribution of how to get a project properly compiled and installed on Mac OS X directly in the project itself?) In any event, I refuse to use either&#8230;though I agree that they have a place for people less comfortable compiling software directly from source code.</p>

	<p>I also prefer not to use pre-compiled binary installations of Open Source tools, unless the maintainer has demonstrated a long-term commitment and expertise in building and maintaining the package. Marc Liyanage did a terrific job with <a href="http://www.entropy.ch/software/macosx/php/"><span class="caps">PHP</span> 5 for Mac OS X</a> releases up through &#8220;Tiger.&#8221; MySQL is doing a great job creating <a href="http://dev.mysql.com/downloads/mysql/5.1.html#macosx-dmg">Mac OS X installer packages for the MySQL database</a>. But many binary installations either make odd decisions about where to put things, or don&#8217;t get maintained, or make module/configuration/packaging decisions different from what I need, or all three.</p>

	<p>So I generally prefer to compile from source code, and install my additions to Mac OS X in <code>/usr/local</code>. Dan Benjamin explains <a href="http://hivelogic.com/articles/view/using_usr_local/">the logic and benefits of <code>/usr/local</code></a> in exhausting detail, I won&#8217;t go into it further. Either you agree, and these instructions may be useful to you, or you don&#8217;t, and you should keep Googling.</p>

	<h2>Prerequisites and Preconditions</h2>

	<ul>
		<li>These instructions are for Mac OS X 10.5 &#8220;Leopard&#8221; only.</li>
		<li>You need to be comfortable, or better yet confident, on the command line.</li>
		<li>You need to have Apple&#8217;s developer tools installed.</li>
		<li>You need to have current versions of Ruby on Rails and Rubygems installed.</li>
		<li>You need to understand what it means to update your $PATH environment, preferrably in <code>~/.bash_login</code>. You <em>do</em> have a <code>~/.bash_login</code> file, right?</li>
	</ul>

	<p>But mostly this document assumes that you&#8217;ve already gone through the <a href="http://hivelogic.com/articles/view/ruby-rails-leopard/">outstanding instructions for installing a complete Ruby on Rails development suite</a> provided at Hivelogic. Indeed, if you didn&#8217;t use those instructions, at least mostly, you probably shouldn&#8217;t use mine either.</p>

	<h2>The Instructions</h2>

	<p>There are two things that need to be installed, the Postgres server and the Ruby gem for the Postgres database driver. You will also want to add a launchd task for starting and stopping the database server.</p>

	<h3>Installing the PostgreSQL Database Server</h3>

	<p>Postgres traditionally runs as a &#8220;daemon,&#8221; or background process, and manages all control to its files, especially its data. This requires that you create a dedicated user account and group, which only Postgres will use. While you <em>could</em> do this in the Accounts preference pane, it&#8217;s better to do this from the command line, so that the system won&#8217;t treat the Postgres user like a normal user. This is done with the Directory Service command line utility, <code>dscl</code>. </p>

	<p>Before creating the Postgres user you need to pick out internal ID numbers for the user and group. You need to find a number that is not currently in use, so start by seeing what <em>is</em> in use. View the list of user accounts and ID numbers (note that some output is elided):</p>

<pre class="terminal">$ <strong>dscl . -list /Users UniqueID</strong>
_amavisd           83
_appowner          87
_appserver         79
[...]
_mysql             74
[...]
alderete           501
daemon             1
nobody             -2
root               0</pre>

	<p>Next the groups:</p>

<pre class="terminal">$ <strong>dscl . -list /Groups PrimaryGroupID</strong>
_amavisd                       83
_appowner                      87
_appserveradm                  81
[...]
_mysql                         74
[...]
tty                            4
utmp                           45
wheel                          0</pre>

	<p>Within these lists you want to find an ID number under 500 that is not in use. My <cite href="http://www.working-software.com/node/30" title="Building and Installing PostgreSQL on Mac OS X">primary source for these instructions</cite> recommending adding 100 to the ID for MySQL, which seems reasonable. It&#8217;s also probably helpful to find a number that is available for both the user and the group ID, so you don&#8217;t have to worry about which is which. From the output above, I chose 174, which met both criteria. </p>

	<p>With chosen user and group IDs, create the user. Here we are creating a user with a $HOME of <code>/usr/local/pgsql</code>, the location where we will be installing Postgres. We&#8217;re also creating the user in such a way that it&#8217;s useful only as a daemon; it is not possible to log in to the system as this user, either at the normal login screen or via ssh.</p>

<pre class="terminal">$ <strong>sudo dscl . -create /Users/postgres UniqueID 174</strong>
$ <strong>sudo dscl . -create /Users/postgres PrimaryGroupID 174</strong>
$ <strong>sudo dscl . -create /Users/postgres HomeDirectory /usr/local/pgsql</strong>
$ <strong>sudo dscl . -create /Users/postgres UserShell /usr/bin/false</strong>
$ <strong>sudo dscl . -create /Users/postgres RealName "PostgreSQL Administrator"</strong>
$ <strong>sudo dscl . -create /Users/postgres Password \*</strong></pre>

	<p>Now verify that the user was created as expected, by reading back the values from the system:</p>

<pre class="terminal">$ <strong>dscl . -read /Users/postgres</strong>
AppleMetaNodeLocation: /Local/Default
GeneratedUID: 0024E028-00A9-48FB-B14A-68026D442870
HomeDirectory: /usr/local/pgsql
Password: *
PrimaryGroupID: 174
RealName:
 PostgreSQL Administrator
RecordName: postgres
RecordType: dsRecTypeStandard:Users
UniqueID: 174
UserShell: /usr/bin/false</pre>

	<p>Follow a similar procedure to create and verify the group:</p>

<pre class="terminal">$ <strong>sudo dscl . -create /Groups/postgres PrimaryGroupID 174</strong>
$ <strong>sudo dscl . -create /Groups/postgres Password \*</strong>
$ <strong>dscl . -read /Groups/postgresAppleMetaNodeLocation: /Local/Default</strong>
GeneratedUID: 7A7F8760-455F-4315-84DD-2DA330AD9845
Password: *
PrimaryGroupID: 174
RecordName: postgres
RecordType: dsRecTypeStandard:Groups</pre>

	<p>With the dedicated user and group created, you can now download, compile, and install Postgres. The <a href="http://www.postgresql.org/download/">source code distribution</a> can be downloaded from one of the Posgresql.org mirrors. If you use Safari to download it, be sure you&#8217;ve turned off the <span class="ui">Open &#8220;safe&#8221; files after downloading</span> option. (And leave it off, it is a <a href="http://daringfireball.net/linked/2006/02/21/safari-shell-scripts">known security risk</a>.)</p>

	<p>Move the archive to your preferred location (such as <code>/usr/local/src</code>), start a Terminal session there, and:</p>

<pre class="terminal">$ <strong>tar zxvf postgresql-8.3.7.tar.gz</strong>
$ <strong>cd postgresql-8.3.7</strong>
$ <strong>./configure</strong>
[...]
$ <strong>make</strong>
[...]
$ <strong>sudo make install</strong>
[...]</pre>

	<p><strong>Note:</strong> there may be options you want to enable with the configure script, such as encryption or authentication methods. Use <code>./configure --help</code> to get more details.</p>

	<p>Installing the software does not actually create a location to store the database data, or initialize the system databases. Do that next:</p>

<pre class="terminal">$ <strong>sudo mkdir /usr/local/pgsql/data</strong>
$ <strong>sudo chown postgres:postgres /usr/local/pgsql/data</strong>
$ <strong>sudo -u postgres /usr/local/pgsql/bin/initdb -D /usr/local/pgsql/data</strong>
Password:
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.
[...]
Success. You can now start the database server using:
    /usr/local/pgsql/bin/postgres -D /usr/local/pgsql/data
or
    /usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data -l logfile start</pre>

	<p>You&#8217;ll want to create a Postgres user (distinct from the system user for Postgres), but before you can do that, you need to start the server. Below I describe how you can have Postgres start automatically when your computer boots, but for now, let&#8217;s just start it up manually:</p>

<pre class="terminal">$ <strong>/usr/local/pgsql/bin/pg_ctl -D /usr/local/pgsql/data start</strong></pre>

	<p>Now create that Postgres user, and a default database for it to use. This is the account that you will eventually use in the database.yml file of your Rails applications. And finally, use the <code>psql</code> command line tool to verify that we&#8217;ve successfully created the user and database.</p>

<pre class="terminal">$ <strong>sudo -u postgres createuser alderete</strong>
$ <strong>createdb</strong>
$ <strong>psql</strong>
Welcome to psql 8.3.7, the PostgreSQL interactive terminal.
[...]
alderete=# <strong>\l</strong>
               List of databases
           Name           |  Owner   | Encoding 
--------------------------+----------+----------
 alderete                 | alderete | UTF8
 postgres                 | postgres | UTF8
 template0                | postgres | UTF8
 template1                | postgres | UTF8
(4 rows)
alderete=# <strong>\q</strong></pre>

	<p><strong>Note:</strong> The above illustrates creating a database user and new database, both named for my Unix account name, &#8220;alderete&#8221;. Using the Unix account name simplifies using Postgres&#8217;s command line tools, you won&#8217;t have to enter the Postgres account name every time. But that will only work if your name is &#8220;alderete&#8221; too. ;-) Instead, use <em>your</em> Unix (or &#8220;short&#8221;) account name instead! This will likely be visible in your command line prompt, but it will also appear in the list of users you generated at the beginning of these instructions.</p>

	<p>Finally, add important Postgres directories to your various system paths. Add the following lines to your <code>~/.bash_login</code> file:</p>


<div class="wp_syntax"><div class="code"><pre class="bash" style="font-family:monospace;"><span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">PATH</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">$PATH</span>:/usr/local/pgsql/bin&quot;</span>
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">MANPATH</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">$MANPATH</span>:/usr/local/pgsql/man&quot;</span>
<span style="color: #7a0874; font-weight: bold;">export</span> <span style="color: #007800;">LD_LIBRARY_PATH</span>=<span style="color: #ff0000;">&quot;<span style="color: #007800;">$LD_LIBRARY_PATH</span>:/usr/local/pgsql/lib&quot;</span></pre></div></div>


	<p>Be sure to source this file to activate the new environment settings:</p>

<pre class="terminal">$ <strong>. ~/.bash_login</strong></pre>

	<h3>Install the Postgres Ruby Gem</h3>

	<p>Normally installing a Ruby gem is pretty simple, but the Postgres database driver has native (compiled) components, and these need to be linked against the Postgres binary. The problem is that by default Postgres is only built for the hardware architecture of your computer (PowerPC <em>or</em> Intel), while the gem appears to try to link against <em>both</em> architectures. This causes an error, and the helpful suggestion that appears with the error message does not in fact work, producing the exact same error message.</p>

	<p>The trick to getting past this is to set the preferred architecture right on the gem install command, like so:</p>

<pre class="terminal">$ <strong>sudo env ARCHFLAGS='-arch i386' gem install \
  postgres -- --with-pgsql-dir=/usr/local/pgsql</strong>
Password:
Building native extensions.  This could take a while...
Successfully installed postgres-0.7.9.2008.01.28
1 gem installed</pre>

	<p>(If you&#8217;re still using a PowerPC-based computer, change &#8220;i386&#8221; to &#8220;ppc&#8221;.)</p>

	<p>This should allow the gem to install successfully.</p>

	<h3>Automatically Starting Postgres at System Startup</h3>

	<p>On my development systems I&#8217;m using the database constantly, and so I have it set to start up automatically, as part of my computer&#8217;s boot process. The &#8220;Leopard&#8221; way to do this is to add a launchd task to your system. This is a text file that you&#8217;ll save on your desktop, and then move to <code>/Library/LaunchDaemons/org.postgresql.dbms.plist</code>. The following was copied from my primary source (see below):</p>


<div class="wp_syntax"><div class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;?xml</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span> <span style="color: #000066;">encoding</span>=<span style="color: #ff0000;">&quot;UTF-8&quot;</span><span style="color: #000000; font-weight: bold;">?&gt;</span></span>
<span style="color: #00bbdd;">&lt;!DOCTYPE plist PUBLIC &quot;-//Apple Computer//DTD PLIST 1.0//EN&quot;</span>
<span style="color: #00bbdd;">    &quot;http://www.apple.com/DTDs/PropertyList-1.0.dtd&quot;&gt;</span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;plist</span> <span style="color: #000066;">version</span>=<span style="color: #ff0000;">&quot;1.0&quot;</span><span style="color: #000000; font-weight: bold;">&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dict<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>Label<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.postgresql.dbms<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>UserName<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>postgres<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>GroupName<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>postgres<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>ProgramArguments<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;array<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/usr/local/pgsql/bin/postmaster<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>-D<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
      <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/usr/local/pgsql/data<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/array<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>StandardErrorPath<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>/usr/local/pgsql/logs/postgresql.log<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/string<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>RunAtLoad<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/key<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
    <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;true</span><span style="color: #000000; font-weight: bold;">/&gt;</span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dict<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/plist<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></div></div>


	<p>Once this file is in place, you can load it by restarting your computer, or with this command:</p>

<pre class="terminal">$ <strong>sudo launchctl load /Library/LaunchDaemons/org.postgresql.dbms.plist</strong></pre>

	<p>If you can believe it, you&#8217;re done. Pour a cold one, or get right to it with <code>rails mynewapp -d postgresql</code>. Good luck!</p>

	<h2>Sources, Citations, and Credits</h2>

	<p>By far the most complete posting on this topic was <br />
Working Software&#8217;s <a href="http://www.working-software.com/node/30">Building and Installing PostgreSQL on Mac OS X</a>, which covered installing Postgres from source completely. While the instructions above are copy/pasted from my Terminal windows, I was copying lines directly from this article to paste <em>into</em> Terminal in the first place.</p>

	<p>The one thing that wasn&#8217;t covered in the above article was installing the Ruby gem for Postgres, which is required to use Postgres in Rails. The normal <code>gem install [gemname]</code> produced errors, and most suggestions I found didn&#8217;t work any better. What finally did work was including the &#8220;-arch i382&#8221; option directly on the gem installation line, which appears to be orginally published towards the bottom of <a href="http://brionesandco.com/ryanbriones/2008/01/postgresql-on-leopard-for-rails-few.html">PostgreSQL on Leopard for Rails: A few notes</a>. (A few other posts repeat this advice, so I&#8217;m not sure who first published it.)</p>

	<p>Finally, although it&#8217;s quite dated, the Apple Developer Connection article <a href="http://developer.apple.com/internet/opensource/postgres.html">PostgreSQL on Mac OS X</a> provides useful background information, and notes about using Postgres from other languages besides Ruby.<p><hr />Copyright &copy; 2001-2011 by <a href="http://aldosoft.com">Aldosoft</a>. All rights reserved. This feed is provided for personal non-commercial use only. If you are not reading this material in your news aggregator, the site you are looking at is guilty of copyright infringement. Please contact legal-2011@aldosoft.com so we can take action immediately.</p></p>]]></content:encoded>
			<wfw:commentRss>http://aldosoft.com/blog/2009/04/installing-postgres-for-ruby-on-mac-os-x/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

