<?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>Cappelen Art &#38; Design &#187; Blog</title>
	<atom:link href="http://cappelendesign.no/blog/feed/" rel="self" type="application/rss+xml" />
	<link>http://cappelendesign.no</link>
	<description></description>
	<lastBuildDate>Sun, 05 Feb 2012 11:15:19 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>Cappelen Art &amp; Design site launched, takes over AcornArtwork</title>
		<link>http://cappelendesign.no/2012/02/cappelen-art-and-design-site-launched-takes-over-acornartwork/</link>
		<comments>http://cappelendesign.no/2012/02/cappelen-art-and-design-site-launched-takes-over-acornartwork/#comments</comments>
		<pubDate>Sat, 04 Feb 2012 13:00:24 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Blog]]></category>

		<guid isPermaLink="false">http://cappelendesign.no/?p=3547</guid>
		<description><![CDATA[Welcome to Cappelen Art &#038; Design&#8217;s website for the sole proprietorship business created in 2011 by Ann Christin Cappelen. As this site launches, another site is meeting its end and is moving in here. As it got complicated and a little pointless to keep two separate portfolio sites, one for the freelance work I do [...]]]></description>
			<content:encoded><![CDATA[<p>Welcome to Cappelen Art &#038; Design&#8217;s website for the sole proprietorship business created in 2011 by Ann Christin Cappelen. As this site launches, another site is meeting its end and is moving in here.</p>

<div class="wp-caption alignright" style="margin-top:5px;"><img title="Bye bye AcornArtwork" src="http://www.cappelendesign.no/wp-content/uploads/misc/acornartwork-cappelendesign.png" alt="" /></div>

<p>As it got complicated and a little pointless to keep two separate portfolio sites, one for the freelance work I do in my business and one for anything else, I decided to combine it all together in one place. That means farewell to my old portfolio site, AcornArtwork. And what&#8217;s better than taking this merge as a challenge to write a customized theme for and using the great <a href="http://www.wordpress.org" title="Wordpress.org" target="_blank">WordPress</a> CMS which does everything I want and need for a business and portfolio site?

<p>All AcornArtwork&#8217;s blog posts have been transferred into Cappelen Art &#038; Design&#8217;s blog, and I hope to post more often. Be it a link to an another site&#8217;s post about something awesome, or the occasional self-written WordPress tutorial.</p>]]></content:encoded>
			<wfw:commentRss>http://cappelendesign.no/2012/02/cappelen-art-and-design-site-launched-takes-over-acornartwork/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Introduction to Linux Commands</title>
		<link>http://cappelendesign.no/2012/02/introduction-to-linux-commands/</link>
		<comments>http://cappelendesign.no/2012/02/introduction-to-linux-commands/#comments</comments>
		<pubDate>Sun, 05 Feb 2012 11:15:19 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Interesting Reading]]></category>
		<category><![CDATA[Resources]]></category>
		<category><![CDATA[linux]]></category>

		<guid isPermaLink="false">http://cappelendesign.no/?p=3662</guid>
		<description><![CDATA[All web developers need to work with a web server, and it&#8217;s then necessary to know a few Linux commands. If you&#8217;re not all too familiar with the Linux OS, this is a must-read. This post goes through basic Linux commands and gets more advanced without becoming to confusing. At the heart of every modern [...]]]></description>
			<content:encoded><![CDATA[<img src="http://cappelendesign.no/wp-content/uploads/2012/01/linusque.jpg-150x150.png" alt="" title="Linus" width="150" height="150" class="alignright size-thumbnail wp-image-3745" />
<p>All web developers need to work with a web server, and it&#8217;s then necessary to know a few Linux commands. If you&#8217;re not all too familiar with the Linux OS, this is a must-read. This post goes through basic Linux commands and gets more advanced without becoming to confusing.</p>
<div class="external-piece">
<p>At the heart of every modern Mac and Linux computer is the “terminal.” The terminal evolved from the text-based computer terminals of the 1960s and ’70s, which themselves replaced punch cards as the main way to interact with a computer. It’s also known as the command shell, or simply “shell.” Windows has one, too, but it’s called the “command prompt” and is descended from the MS-DOS of the 1980s.</p>

<p>Mac, Linux and Windows computers today are mainly controlled through user-friendly feature-rich graphical user interfaces (GUIs), with menus, scroll bars and drag-and-drop interfaces. But all of the basic stuff can still be accomplished by typing text commands into the terminal or command prompt.</p>
</div>]]></content:encoded>
			<wfw:commentRss>http://cappelendesign.no/2012/02/introduction-to-linux-commands/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>WordPress Theme Lesson 14: Finalizing Your Theme</title>
		<link>http://cappelendesign.no/2012/01/wordpress-theme-lesson-14-finalizing-your-theme/</link>
		<comments>http://cappelendesign.no/2012/01/wordpress-theme-lesson-14-finalizing-your-theme/#comments</comments>
		<pubDate>Sat, 14 Jan 2012 23:31:27 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Web Coding]]></category>
		<category><![CDATA[seo]]></category>
		<category><![CDATA[wordpress]]></category>
		<category><![CDATA[wordpress theme tutorial]]></category>

		<guid isPermaLink="false">http://www.acornartwork.com/blog/?p=2888</guid>
		<description><![CDATA[This final lesson is about improving pieces of our theme. We add a favorite icon, screenshot for WordPress admin, looking at other WordPress functions to make some parts better, and finally take a look at plugins you can install to make the theme better. It hasn&#8217;t been easy to find a good closing point on [...]]]></description>
			<content:encoded><![CDATA[<div class="wp-caption alignleft"><img title="Wordpress theme tutorial" src="http://www.cappelendesign.no/wp-content/uploads/misc/wp-theme-tutorial/Wordpress_tutorial.png" alt="" /></div>
<p>This final lesson is about improving pieces of our theme. We add a favorite icon, screenshot for WordPress admin, looking at other WordPress functions to make some parts better, and finally take a look at plugins you can install to make the theme better.</p>

<p>It hasn&#8217;t been easy to find a good closing point on a WordPress theme tutorial because there is so much more you can do. But the initial goal of this tutorial was an introduction to create your own theme from scratch, getting a strong foundation, and getting familiar with the WordPress API. Hopefully it has been enough to achieve an overview of the path ahead on what you want to learn next, and you now have a basic theme as a starting point. The internet is full of tutorials for pieces of WordPress theme and plugin coding &#8211; just <a href="http://www.google.com" target="_blank" title="just Google it">Google it</a>.</p>
<span id="more-2888"></span>

<h3>Small Improvements to our Theme</h3>
<h4 style="padding-top:10px;font-size:12px;">Favorite icon</h4>
<p>Most web pages use a <a href="http://en.wikipedia.org/wiki/Favicon" target="_blank" title="Favorite icon explained at Wikipedia">favorite icon/bookmark icon</a> which appears next to your adress bar and in the bookmark list if the page is bookmarked. Normally it is a 16&#215;16 pixels image, and I&#8217;ve created a ridicolously simple one for our theme, following the Space invaders theme.</p>
<img src="http://www.cappelendesign.no/wp-content/uploads/misc/wp-theme-tutorial/theme-favicon.png" title="Theme Favicon" style="float: left;padding-top:2px;padding-right:5px;" />
<p>Right click on the image to the left and choose Save as&#8230; Save it in your theme&#8217;s <code>images</code> folder.</p>
<p>To define a favorite icon for your site add a <code>&lt;link rel="shortcut icon"&gt;</code> line in the header. So for our theme add the following line in <code>header.php</code>, somewhere inside the <code>&lt;head&gt;</code> block.</p>
<h4 style="font-size:13px;clear:both;">header.php:</h4>
<div class="wp_syntax">
<pre class="php">
&lt;link <span class="b">rel</span>=<span class="r">"shortcut icon"</span> <span class="b">href</span>=<span class="r">"</span><span class="y">&lt;?php <span class="l">bloginfo</span>(<span class="r">'stylesheet_directory'</span>) ?&gt;</span><span class="r">/images/theme-favicon.png"</span> />
</pre>
</div>

<h4 style="padding-top:10px;font-size:12px;">Theme Screenshot</h4>
<p>If you take a look in <code>WordPress admin &raquo; Appearance &raquo; Themes</code> you&#8217;ll see that most themes listed have a screenshot. It makes it look more appealing and interesting, and gives you a quick view of what the theme looks like without previewing or activating it.</p>
<div class="wp-caption alignleft" style="width: 110px;"><a  class="shutterset_set_wptut_lesson14" href="http://www.cappelendesign.no/wp-content/uploads/misc/wp-theme-tutorial/theme-screenshot.png"><img src="http://www.cappelendesign.no/wp-content/uploads/misc/wp-theme-tutorial/thumbs/theme-screenshot.png" title="Theme screenshot in WordPress Admin &raquo; Appearance &raquo; Themes" /></a></div>
<p>In order to give your theme such a screenshot, you need an image named <code>screenshot</code> in your theme&#8217;s folder (theme root folder, <strong>not</strong> in the <code>images</code> or some other folder).</p>
<p>I normally take a screenshot of the finished site and resize it so it fits 300&#215;225 pixels. I&#8217;ve <a href="http://www.cappelendesign.no/wp-content/uploads/misc/wp-theme-tutorial/theme-screenshot.png" target="_blank" title="Download screenshot">made one for you here</a>. Right click on the link, choose Save link as.. and save it in your theme&#8217;s folder. It will automatically appear along your theme.</p>

<h4 style="padding-top:10px;font-size:12px;">Better Pagination</h4>
<p>In our <code>index.php</code>, <code>search.php</code>, and <code>archive.php</code> we&#8217;ve used the most common pagination method used in blogs: displaying a link to newer and older entries by using <code>posts_nav_link()</code>. But in many cases numbered pagination works much better, especially for jumping several pages at once. I use numbered pagination on this and my other blogs.</p>
<p>There&#8217;s a well-known plugin, <a href="http://wordpress.org/extend/plugins/wp-pagenavi/" target="_blank" title="Download WP-PageNavi">WP-PageNavi</a>, which provides you with a customizable numbered pagination system, but if you prefer not to install a plugin WordPress offers a function which pretty much does the same: <a href="http://codex.wordpress.org/Function_Reference/paginate_links" target="_blank" title="WordPress Codex reference: paginate_links">paginate_links()</a>.</p>
<div class="wp-caption alignleft" style="width: 160px;"><a  class="shutterset_set_wptut_lesson14" href="http://www.cappelendesign.no/wp-content/uploads/misc/wp-theme-tutorial/wppagenavi.jpg"><img src="http://www.cappelendesign.no/wp-content/uploads/misc/wp-theme-tutorial/thumbs/wppagenavi.jpg" title="A better navigation" /></a></div>
<p>I won&#8217;t go through the code for replacing the pagination, since the Codex page pretty much explains how to do it. By providing parameters and adding some styling you achieve pretty much the same as with the WP-PageNavi plugin. The advantage of writing the pagination yourself is embedding it in your theme so you won&#8217;t need to install the plugin again if you change to another WordPress installation.</p>

<h3>Plugins to Improve Your Theme</h3>

<p>There are thousands of plugins available (in writing there are 17,800 according to the <a href="http://wordpress.org/extend/plugins/" target="_blank" title="WordPress plugin directory">WordPress plugin directory</a>) to improve your site, so in this section I&#8217;ve simply focused on fulfilling the most basic needs &#8211; such as image viewers, social buttons and search engine optimization. A thumb rule is, if there is a general need for something in WordPress, most likely there exist a plugin which does just that.</p>

<h4 style="padding-top:10px;font-size:12px;">Image Viewer</h4>
<p>Depending on your WordPress settings, when clicking on an image you&#8217;ve uploaded and attached to your post, it&#8217;ll either lead to an attachment page for the image or display the full-size image. Either way the reader has to use the back-button in the browser to continue reading the post. It&#8217;s much better to use some kind of javascript image viewer which overlays the image ontop of the page without taking you to another page.</p>

<p>There are many image viewer plugins available for WordPress. On this site I use <a href="http://wordpress.org/extend/plugins/shutter-reloaded/" target="_blank" title="Download Shutter Reloaded">Shutter Reloaded</a>, but <a href="http://wordpress.org/extend/plugins/lightbox-2/" target="_blank" title="Download Lightbox 2">Lightbox 2</a>, <a href="http://wordpress.org/extend/plugins/thickbox/" target="_blank" title="Download Thickbox">Thickbox</a>, <a href="http://wordpress.org/extend/plugins/lightbox-plus/" target="_blank" title="Download Lightbox Plus">Lightbox Plus</a>, <a href="http://wordpress.org/extend/plugins/fancybox-for-wordpress/" target="_blank" title="FancyBox plugin for WordPress">FancyBox</a>, or any other works just as well. Just choose one which fits the needs of your site. I like Shutter Reloaded because the navigation buttons always displays at the same place regardless of the image size, it shows the image as large as possible to fit in your screen, and has a button for displaying it in full size if applicable.</p>

<h4 style="padding-top:10px;font-size:12px;">Social Buttons</h4>
<p>A great way to spread your posts on the internet is to provide social share buttons &#8211; buttons on each post which readers can share your post on their Facebook wall, Like, tweet, add to favorites, and more. Again there are plenty of WordPress plugins which takes care of this for you. <a href="http://wordpress.org/extend/plugins/sexybookmarks/" target="_blank" title="Download SexyBookmarks">SexyBookmarks</a> is a tastefully designed and highly customizable sharing plugin. <a href="http://wordpress.org/extend/plugins/socialize/" target="_blank" title="Download Socialize">Socialize</a> is another. On this blog I use an account at <a href="http://www.addthis.com/" target="_blank" title="Go to Addthis.com">AddThis</a> and embed some code in my theme files for the row of sharing buttons you see on the bottom of this post.</p>

<h4 style="padding-top:10px;font-size:12px;">Gallery</h4>
<p>At some point you&#8217;d most likely need to display a series of images, and for that you can use the built-in WordPress gallery function or a gallery plugin. I highly recommend <a href="http://wordpress.org/extend/plugins/nextgen-gallery/" target="_blank" title="Download NextGen Gallery">NextGen Gallery</a>. In my opinion there is no better plugin.</p>

<h4 style="padding-top:10px;font-size:12px;">Search Engine Optimization (SEO)</h4>
<p>Seach Engine Optimization is important if you want your site to appear on search sites such as <a href="http://www.google.com" target="_blank" title="Google">Google</a>, or any other internet search engine. Enrich your blog with meta information by providing better titles (different from your post title), a description and keywords. To learn more about SEO and WordPress, check out <a href="http://codex.wordpress.org/Search_Engine_Optimization_for_Wordpress" target="_blank" title="WordPress Codex: Search Engine Optimization for WordPress">WordPress Codex on SEO</a>.</p>

<p>In our theme we&#8217;ve already made sure all pages get extensive descriptive titles by using <code>bloginfo('name'); wp_title();</code> inside the <code>&lt;title&gt;</code> tag. But we haven&#8217;t added any <code>meta</code> tags, such as description or keywords, because the content may vary from each post or template. So it&#8217;s a good idea to use a plugin which takes care of this variation for us. I&#8217;ve used both <a href="http://wordpress.org/extend/plugins/all-in-one-seo-pack/" target="_blank" title="Download All in One SEO Pack">All in One SEO Pack</a> and <a href="http://wordpress.org/extend/plugins/seo-ultimate/" target="_blank" title="Download SEO Ultimate">SEO Ultimate</a>, both great SEO plugins.</p>

<h4 style="padding-top:10px;font-size:12px;">Caching</h4>
<p>To speed up the loading time of your site you can use cache plugins to temporarily store pages to make it faster the next time someone loads it. <a href="http://wordpress.org/extend/plugins/quick-cache/" target="_blank" title="Download Quick Cache">Quick Cache</a>, <a href="http://wordpress.org/extend/plugins/wp-super-cache/" target="_blank" title="Download WP Super Cache">WP Super Cache</a>, <a href="http://wordpress.org/extend/plugins/w3-total-cache/" target="_blank" title="Download W3 Total Cache">W3 Total Cache</a> are three great cache plugins, with each their own advantages and disadvantages. I don&#8217;t recommend using W3 Total Cache on a WordPress multisite.</p>

<h3>Tutorial Conclusion</h3>
<p>As mentioned before it&#8217;s difficult to find a ending point in a WordPress theme tutorial since it covers such an excellent and complex CMS. But I need to stop somewhere &#8211; the initial goal was to show the most basic of writing a WordPress theme without it becoming to long and boring, or focused on specific areas you might not find interesting or relevant. WordPress can do so much more than this, and I hope you&#8217;ve been encouraged to dig deeper and learn more rather than finding it to complex and stupid to bother. At least you have a basic theme as a starting point which you can copy and modify instead of writing a new theme from scratch all over again.</p>

<p>If you haven&#8217;t followed all the steps, seem to have misunderstood something or might be missing something, you can download the theme complete with all files here:</p>
<p style="font-size:14px;border-top:1px dotted #BBB;border-bottom:1px dotted #BBB; padding:15px 0;margin:10px 0;text-align:center;"><strong><a href="http://www.cappelendesign.no/wp-content/uploads/misc/wp-theme-tutorial/wpthemetutorial-finaltheme.zip" title="Download the source code">Click here to download the source files for our theme.</a></strong></p>

<p>Thank you for reading, and I hope this has been useful!</p>

<p><em>This concludes the final lesson of the <a href="http://cappelendesign.no/?p=1229" title="WordPress Theme Tutorial in 14 Lessons">WordPress Theme Tutorial</a>. Go back to the tutorial&#8217;s <a href="http://cappelendesign.no/?p=1229#toc" title="WordPress Theme Tutorial in 14 Lessons Table of Contents">Table of Contents</a>, or start writing a theme of your own!</em></p>]]></content:encoded>
			<wfw:commentRss>http://cappelendesign.no/2012/01/wordpress-theme-lesson-14-finalizing-your-theme/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>WordPress Theme Lesson 13: Page Templates</title>
		<link>http://cappelendesign.no/2012/01/wordpress-theme-lesson-13-page-templates/</link>
		<comments>http://cappelendesign.no/2012/01/wordpress-theme-lesson-13-page-templates/#comments</comments>
		<pubDate>Sat, 14 Jan 2012 22:23:24 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Web Coding]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[wordpress]]></category>
		<category><![CDATA[wordpress theme tutorial]]></category>

		<guid isPermaLink="false">http://www.acornartwork.com/blog/?p=1896</guid>
		<description><![CDATA[One of the last things I&#8217;m going through in this tutorial is how you can create page templates for even more variability in your site. If you&#8217;ve ever needed or wanted to code a single page doing or showing something else than the default pages we&#8217;ve looked at up to now, page templates is a [...]]]></description>
			<content:encoded><![CDATA[<div class="wp-caption alignleft"><img title="Wordpress theme tutorial" src="http://www.cappelendesign.no/wp-content/uploads/misc/wp-theme-tutorial/Wordpress_tutorial.png" alt="" /></div>
<p>One of the last things I&#8217;m going through in this tutorial is how you can create page templates for even more variability in your site. If you&#8217;ve ever needed or wanted to code a single page doing or showing something else than the default pages we&#8217;ve looked at up to now, page templates is a way to go.</p>

<p>In this lesson we&#8217;ll create two page templates. We will create an archive page (different from the default archive page, <code>archive.php</code>), which on a single page lists <em>all</em> posts in a short form, and below displays tag clouds &#8211; one for categories and one for tags. This gives the reader easy access to all information on your site, and is a good example for using in your search results or 404 error templates. The second page template we will create is an example of a front page for our blog, which displays a different featured posts slider than we created in <a href="http://cappelendesign.no/?p=2711" title="WordPress Theme Lesson 12: Featured Posts, Dealing with Multiple Loops">the last lesson</a> and some text. Many use a page template to create a different front page, as opposed to the traditional listing of recent posts. In both page templates we will go full-width, skipping the sidebar. As default all of our pages or posts displays the sidebar, so we can simply tell our page templates to skip the sidebar instead of modifying our existing theme files.</p>
<span id="more-1896"></span>
<p>By now you should know that <a href="http://codex.wordpress.org/Pages" target="_blank" title="Wordpress on Pages">pages</a> are different than posts in WordPress. Pages, which cannot be assigned categories or tags but can be organized hierarchically, are aften used for static content. But whereas we can&#8217;t assign templates to posts, we can do so with pages.</p>

<div class="wp-caption alignleft" style="width: 186px; margin-top: 10px;"><img title="The page template selector" src="http://www.cappelendesign.no/wp-content/uploads/misc/wp-theme-tutorial/pagetemplate.jpg" alt="" /><p class="wp-caption-text">The page template selector</p></div>
<p>The process of assigning a page template to a page is pretty easy. When you edit a page in WordPress admin, to the right you&#8217;ll find the box &#8220;Page attributes&#8221; below the &#8220;Publish&#8221; box. From here you can choose templates from the &#8220;Template&#8221; dropdown. <em>But the dropdown is only visible if your theme contains one or more page templates</em>. We haven&#8217;t created any page templates in our theme, so the dropdown is not visible when we edit pages with our theme active.</p>

<p>In a page template you can basically do anything. You can choose to skip the sidebar, use a different footer, give it a whole different look by using a different HTML structure and styles, write a different loop, skip to include the title or content in the page assigned to the template, or choose to print some of it. It&#8217;s up to you.</p>

<h4 style="padding-top:10px;">The Basics of Creating a Page Template</h4>
<p>Creating a page template is as simple as creating a file in your theme folder, and in the file add a line of code enclosed in comments to tell WordPress that this is a page template by giving it a name.</p>

<p>But first there&#8217;s a couple of things you should know about when it comes to naming your page template files. Page template files can be named anything you want, but must not have the same name as one of WordPress&#8217;s <a href="http://codex.wordpress.org/Theme_Development#Template_Files_List" target="_blank" title="See a list of WordPress' reserved file names">reserved file names</a>. Also, the page template name can not have the same name as any of your WordPress&#8217;s page slugs. For instance if you have a page with the title &#8220;About&#8221;, its slug probably is &#8220;about&#8221; (you can find the slug just below the title in Edit Page). If you create a page template named <code>about.php</code>, you will achieve an 404 error or experience pagination errors. The best you can do is prefixing the page template files with your theme&#8217;s slug, thus making sure the names are not conflicting with anything else.</p>

<p>The lines of code required for WordPress to recognize your file as a page template are:</p>
<div class="wp_syntax">
<pre class="php">
<span class="y">&lt;?php
<span class="g">/*
Template Name: Your Template Name
*/</span>
?&gt;</span>
</pre>
</div>
<p>Always place this PHP piece of code <em>on top</em> of all your page templates. That&#8217;s it! The given name appears in the Template dropdown in Edit Page.</p>
<p>Let&#8217;s begin with the first of our two templates, the full-width special archive page.</p>

<h3>Creating a Full-width Archives Page Template With Tag Clouds</h3>
<p>First of all, we want a full-width page, meaning no sidebar. How do we do that? Simple, we simply skip writing <code>get_sidebar()</code>. We also use some different <code>&lt;div&gt;</code> wrappings so we can style the full-width container properly.</p>
<p>The title and page content we fill in Edit Page in WordPress admin are irrelevant in this template, we choose not to display them. We will also defer using the loop (containing the single page) WordPress provides us, instead we will create a custom loop fetching all published posts. Then we use WordPress&#8217;s built-in function for displaying tag clouds &#8211; one for categories and one for tags.</p>
<p>Create an empty file in your theme folder, and call it <code>wpthemetutorial-fullarchives.php</code>.</p>
<h4 style="font-size:13px;clear:both;">wpthemetutorial-fullarchives.php:</h4>
<div class="wp_syntax">
<pre class="php">
<span class="y">&lt;?php
<span class="g">/* 
Template Name: Full-width Archives
*/</span>
<span class="l">get_header</span>(); ?&gt;</span>
				
	&lt;div <span class="b">id</span>=<span class="r">"content-full"</span>&gt;
		<span class="y">&lt;?php</span>
		<span class="y"><span class="b">$debut</span> = 0;  <span class="g">//first post to be displayed</span></span>
		<span class="y"><span class="b">$numposts</span> = <span class="b">$wpdb-&gt;get_var</span>(<span class="r">"SELECT count(*) FROM $wpdb-&gt;posts WHERE post_status = 'publish' AND post_type = 'post'"</span>);</span>
		<span class="y"><span class="l">if</span> (0 &lt; <span class="b">$numposts</span>) <span class="b">$numposts</span> = <span class="l">number_format</span>(<span class="b">$numposts</span>); ?&gt;</span>
		&lt;h2&gt;<span class="y">&lt;?php echo <span class="b">$numposts</span>; ?&gt;</span> posts in this blog since October 19 2009&lt;/h2&gt;
		&lt;ul <span class="b">id</span>=<span class="r">"archive-list"</span>&gt;
			<span class="y">&lt;?php</span>
			<span class="y"><span class="b">$myposts</span> = <span class="l">get_posts</span>(<span class="r">"numberposts=-1&#038;offset=$debut"</span>);</span>
			<span class="y"><span class="l">foreach</span> (<span class="b">$myposts</span> as <span class="b">$post</span>) { ?&gt;</span>
			&lt;li&gt;<span class="y">&lt;?php echo <span class="l">the_time</span>(<span class="r">'d.m.Y'</span>); ?&gt;</span>: &lt;a <span class="b">href</span>=<span class="r">"</span><span class="y">&lt;?php echo <span class="l">the_permalink</span>(); ?&gt;</span><span class="r">"</span>&gt;<span class="y">&lt;?php echo <span class="l">the_title</span>(); ?&gt;</span>&lt;/a&gt;&lt;/li&gt;
			<span class="y">&lt;?php } ?&gt;</span>
		&lt;/ul&gt;
					
		&lt;h3 <span class="b">class</span>=<span class="r">"fullwidth-band"</span>&gt;Browse Posts by Tag:&lt;/h3&gt;
		&lt;div <span class="b">class</span>=<span class="r">"tag-cloud"</span>&gt;<span class="y">&lt;?php <span class="l">wp_tag_cloud</span>(<span class="l">array</span>(<span class="r">'taxonomy'</span> => <span class="r">'post_tag'</span>, <span class="r">'number'</span> => 0)); ?&gt;</span>&lt;/div&gt;
                    
		&lt;h3 <span class="b">class</span>=<span class="r">"fullwidth-band"</span>&gt;Browse Posts by Category:&lt;/h3&gt;
		&lt;div <span class="b">class</span>=<span class="r">"tag-cloud"</span>&gt;<span class="y">&lt;?php <span class="l">wp_tag_cloud</span>(<span class="l">array</span>(<span class="r">'taxonomy'</span> => <span class="r">'category'</span>, <span class="r">'number'</span> => 0)); ?&gt;</span>&lt;/div&gt;
		
	&lt;/div&gt;		
	
<span class="y">&lt;?php <span class="l">get_footer</span>(); ?&gt;</span>
</pre>
</div>
<p>After the page template name, we include the header and define a different content-wrapping <code>&lt;div&gt;</code> as to style it full-width. Then, we create a SQL query to the WordPress posts-table counting how many posts we have (the SQL query is <em>not</em> used for fetching posts). We only count posts, not pages, and only posts that are published. The number is used in the title, stating the total of posts published. I&#8217;ve written the date I installed my local WordPress so you should replace the date with a date that fits for you or skip it.</p>
<p>Then, in a list, we fetch posts using <code>get_posts()</code>. The crucial argument <code>numberposts=-1</code> makes WordPress return all posts regardless of pagination limits. The <code>$offset</code> makes sure we start offsetting at the first post. For each post we print the date it was created with its title, linking to the post.</p>

<div class="wp-caption alignright" style="width: 135px;"><a href="http://www.cappelendesign.no/wp-content/uploads/misc/wp-theme-tutorial/screenshot-template-archives.jpg" target="_blank" class="shutterset_set_wptutlesson13"><img title="Screenshot of our full-width archive template" src="http://www.cappelendesign.no/wp-content/uploads/misc/wp-theme-tutorial/thumbs/screenshot-template-archives.jpg" alt="" /></a></div>

<p>After the loop we look into tag clouds. Luckily creating tag clouds is pretty simple with WordPress function <code>wp_tag_cloud()</code>. We provide the taxonomy type as argument, which can be either <code>category</code> or <code>post_tag</code> (or a custom taxonomy, but that&#8217;s beyond this tutorial). Setting the <code>number</code> to 0 will dispay all tags or categories, not limited to WordPress&#8217;s rule of showing a maximum of 45 most used tags.</p>

<p>Go to WordPress admin and add or edit one of your pages, and select &#8220;Full-width Archives&#8221; in the Template dropdown. Save, preview your page and you are looking at pretty much all content in your blog gathered in one full-width page. It doesn&#8217;t look any good though, so let&#8217;s add some styling in our <code>style.css</code> to make it look like my screenshot to the right (unpixelated obviously). <a href="http://www.cappelendesign.no/wp-content/uploads/misc/wp-theme-tutorial/images-lesson-13.zip" title="Download images used in this lesson">Click here</a> to download the image used below, unzip and put it in the <code>images</code> folder.</p>
<h4 style="font-size:13px;clear:both;">style.css:</h4>
<div class="wp_syntax">
<pre class="css">
<span class="g">/*** FULL WIDTH ARCHIVE ***/</span>
#<span class="l">content-full</span> {
	<span class="l">width</span>: 780px;
	<span class="l">margin</span>: 0 auto;
}
<span class="l">h3.fullwidth-band</span> {
	<span class="l">background</span>: url(<span class="r">"images/content-full-band.png"</span>) no-repeat top left;
	<span class="l">margin</span>: 0 -30px;
	<span class="l">padding</span>: 9px 10px 12px 35px;
	<span class="l">color</span>: #DEE9F5;
}
<span class="l">ul#archive-list</span> {
	<span class="l">list-style-type</span>: none;
	<span class="l">padding</span>: 10px 0 20px 0;
}
.<span class="l">tag-cloud</span> { <span class="l">margin</span>: 0 0 20px 0; }
</pre>
</div>
<h4>WordPress Codex Function references:</h4>
<ul>
<li><a href="http://codex.wordpress.org/Function_Reference/get_header" target="_blank" title="Wordpress Function Reference: get_header">get_header</a> and <a href="http://codex.wordpress.org/Function_Reference/get_footer" target="_blank" title="Wordpress Function Reference: get_footer">get_footer</a></li>
<li><a href="http://codex.wordpress.org/Function_Reference/get_posts" target="_blank" title="Wordpress Function Reference: get_posts">get_posts</a></li>
<li><a href="http://codex.wordpress.org/Function_Reference/the_time" target="_blank" title="Wordpress Function Reference: the_time">the_time</a>, <a href="http://codex.wordpress.org/Function_Reference/the_permalink" target="_blank" title="Wordpress Function Reference: the_permalink">the_permalink</a>, and <a href="http://codex.wordpress.org/Function_Reference/the_title" target="_blank" title="Wordpress Function Reference: the_title">the_title</a></li>
<li><a href="http://codex.wordpress.org/Function_Reference/wp_tag_cloud" target="_blank" title="Wordpress Function Reference: wp_tag_cloud">wp_tag_cloud</a></li>
</ul>

<h3>Creating a Front Page Template</h3>
<p>To close up on this tutorial, I decided to create a page template which utilizes much of what we&#8217;ve been through in this tutorial. We are to create a front page template with a title, a different slider than our featured posts slider, and some welcome text below. Instead of hard-coding the title and welcome text, we display the page&#8217;s title and content, making it easy to modify as we don&#8217;t need to edit the theme files. Although this front page isn&#8217;t according to our initial design, it gives you an idea of how you can make your WordPress installation look less like a blog and more like a site.</p>

<p>As for this page template I&#8217;ve chosen to use a different jQuery library than we <a href="http://cappelendesign.no/?p=2711" title="WordPress Theme Lesson 12: Featured Posts, Dealing with Multiple Loops" target="_blank">used in our featured posts section</a>, partly because we only want to display three posts with a different sliding effect, and partly to ensure that you remember how to include scripts properly in WordPress. The idea of the slider is to display three posts, with titles to the left, and the current post thumbnail on the right-hand side. For making this pretty we need to add a new image size, like we did in <a href="http://cappelendesign.no/?p=1654" title="WordPress Theme Lesson 8: Post Thumbnails" target="_blank">lesson 8</a>.</p>

<p>When it comes to term with choosing which posts to display we could create a new category called something appropriate such as &#8220;Front page&#8221;, or we could use the Featured category we used in <a href="http://cappelendesign.no/?p=2711" title="WordPress Theme Lesson 12: Featured Posts, Dealing with Multiple Loops" target="_blank">lesson 12</a>. Another possibilty to choose which posts and thumbnails to display is using <a href="http://codex.wordpress.org/Custom_Fields" target="_blank" title="WordPress Codex on Custom fields">custom fields</a> in the page assigned to the front page template &#8211; but we haven&#8217;t been through custom fields in this tutorial so I&#8217;ve chosen to fetch the three recent posts from our existing Featured category.</p>

<p>Since we&#8217;ve been through most of this before, I won&#8217;t explain in details what we&#8217;re doing, refer to previous lessons if you get stuck.</p>

<p>First, we add our new image size in our <code>functions.php</code> (remember to generate thumbnails afterwards):</p>
<h4 style="font-size:13px;clear:both;">functions.php:</h4>
<div class="wp_syntax">
<pre class="php">
<span class="l">add_image_size</span>(<span class="r">'frontpage-template-thumbnail'</span>, 400, 260, TRUE);
</pre>
</div>
<p>Next, we enqueue the necessary javascript libraries. The jQuery library we are to use, <a href="http://jqueryui.com/demos/tabs/" target="_blank" title="jQuery UI Tabs">jQuery UI Tabs</a>, is inside the <a href="http://jqueryui.com/" target="_blank" title="jQuery UI's web site">jQuery UI</a> library. The jQuery UI library is included with WordPress, which means we only need to enqueue it. So Tabs&#8217;s dependencies are jQuery UI and then the jQuery core library. Find our existing function <code>wpthemetutorial_add_js()</code> in <code>functions.php</code>, where we registered and enqueued the jQuery Tools, and add this line of code <em>right below</em> the enqueuing of jQuery Tools:</p>
<h4 style="font-size:13px;clear:both;">functions.php:</h4>
<div class="wp_syntax">
<pre class="php">
<span class="l">wp_enqueue_script</span>(<span class="r">'jquery-ui-tabs'</span>, <span class="l">null</span>, <span class="l">array</span>(<span class="r">'jquery-ui-core'</span>, <span class="r">'jquery'</span>));
</pre>
</div>
<p>We need to initialize Tabs by calling the function <code>tabs()</code> on the element we want to activate Tabs onto. We do this in the <code>&lt;head&gt;</code> tag inside a <code>jQuery(document).ready()</code> function. We added this in <a href="http://cappelendesign.no/?p=2711" title="WordPress Theme Lesson 12: Featured Posts, Dealing with Multiple Loops" target="_blank">lesson 12</a> when initializing jQuery Scrollable and Navigator, and we can add Tabs in the same function. In <code>functions.php</code> find our <code>wpthemetutorial_add_init_js()</code> function, and inside the <code>jQuery(document).ready()</code> function, add this line after the initialization of Scrollable:</p>
<h4 style="font-size:13px;clear:both;">functions.php:</h4>
<div class="wp_syntax">
<pre class="php">
jQuery(<span class="r">"#tabs"</span>).tabs().tabs(<span class="r">'rotate'</span>, 5000);
</pre>
</div>
<p>Now all that&#8217;s left is to actually create the page template.</p>

<p>Create a new file in your theme directory, and name it <code>wpthemetutorial-frontpage.php</code>.</p>
<h4 style="font-size:13px;clear:both;">wpthemetutorial-frontpage.php:</h4>
<div class="wp_syntax" style="height:450px;overflow:auto;">
<pre class="php">
<span class="y">&lt;?php
<span class="g">/*
Template Name: Front Page Template
*/</span>
<span class="l">get_header</span>(); ?&gt;</span>
&lt;div <span class="b">id</span>=<span class="r">"content-full"</span>&gt;
	&lt;h2 <span class="b">class</span>=<span class="r">"frontheading"</span>&gt;<span class="y">&lt;?php <span class="l">the_title</span>(); ?&gt;</span>&lt;/h2&gt;
	&lt;div <span class="b">id</span>=<span class="r">"tabs"</span>&gt;
		&lt;ul&gt;
			<span class="y">&lt;?php</span>
			<span class="y"><span class="b">$my_query</span> = new <span class="l">WP_Query</span>(<span class="r">'category_name=Featured&#038;posts_per_page=3'</span>);</span>
			<span class="y"><span class="b">$counter</span> = 0;</span>
			<span class="y"><span class="l">while</span> (<span class="b">$my_query->have_posts</span>()) : <span class="b">$my_query->the_post</span>(); ?&gt;</span>
			&lt;li&gt;&lt;a <span class="b">href</span>=<span class="r">"#tabs-</span><span class="y">&lt;?php echo <span class="b">$counter</span>++; ?&gt;</span><span class="r">"</span>&gt;&lt;span <span class="b">class</span>=<span class="r">"capitalize"</span>&gt;<span class="y">&lt;?php <span class="l">the_title</span>(); ?&gt;</span>&lt;/span&gt;<span class="y">&lt;?php <span class="l">the_excerpt</span>(); ?&gt;</span>&lt;/a&gt;&lt;/li&gt;
			<span class="y">&lt;?php <span class="l">endwhile</span>; <span class="b">$counter</span> = 0; <span class="g">//reset counter</span> ?&gt;</span>
		&lt;/ul&gt;

		<span class="y">&lt;?php </span>
		<span class="y"><span class="b">$my_query</span> = new <span class="l">WP_Query</span>(<span class="r">'category_name=Featured&#038;posts_per_page=3'</span>);</span>
		<span class="y"><span class="l">while</span> (<span class="b">$my_query->have_posts</span>()) : <span class="b">$my_query->the_post</span>(); ?&gt;</span>
		&lt;div <span class="b">id</span>=<span class="r">"tabs-</span><span class="y">&lt;?php echo <span class="b">$counter</span>++; ?&gt;</span><span class="r">"</span>&gt;
			<span class="y">&lt;?php <span class="l">if</span> (<span class="l">has_post_thumbnail</span>()) { <span class="l">the_post_thumbnail</span>(<span class="r">'frontpage-template-thumbnail'</span>); } ?&gt;</span>
		&lt;/div&gt;
		<span class="y">&lt;?php <span class="l">endwhile</span>; ?&gt;</span>
	&lt;/div&gt;

	<span class="y">&lt;?php <span class="l">if</span> (<span class="l">have_posts</span>()) : <span class="l">while</span> (<span class="l">have_posts</span>()) : <span class="l">the_post</span>(); ?&gt;</span>
	&lt;div <span class="b">class</span>=<span class="r">"fullwidth-text"</span>&gt;
		<span class="y">&lt;?php <span class="l">the_content</span>(); ?&gt;</span>
	&lt;/div&gt;		
	<span class="y">&lt;?php <span class="l">endwhile</span>; <span class="l">endif</span>; ?&gt;</span>			
&lt;/div&gt;
<span class="y">&lt;?php <span class="l">get_footer</span>(); ?&gt;</span>
</pre>
</div>
<p>This code prints the title, excerpt and post thumbnail to the three most recent posts in the Featured category, along with some HTML tags necessary for jQuery Tabs. Below we have the normal loop which contains the page, and from that we print its content. Finally, to make it look good we need some styling. In our <code>style.css</code> add:
<h4 style="font-size:13px;clear:both;">style.css:</h4>
<div class="wp_syntax" style="height:350px;overflow:auto;">
<pre class="css">
<span class="g">/*** FRONT PAGE ***/</span>
<span class="l">h2.frontheading</span> { <span class="l">padding-bottom</span>: 15px; <span class="l">font-size</span>: 22px; }
.<span class="l">fullwidth-text</span> { <span class="l">padding</span>: 10px; }
.<span class="l">capitalize</span> { <span class="l">text-transform</span>: uppercase; <span class="l">font-weight</span>: bold; }
.<span class="l">fullwidth-column</span> {
	<span class="l">width</span>: 350px;
	<span class="l">float</span>: left;
	<span class="l">margin</span>: 5px 30px 0 0;
}

<span class="g">/* Jquery UI tabs */</span>
.<span class="l">ui-tabs .ui-tabs-hide</span> { <span class="l">display</span>: none; }
#<span class="l">tabs</span> {
	<span class="l">font-size</span>: 11px;
	<span class="l">border</span>: 1px solid #dadcde;
	<span class="l">background-color</span>: #ffffff;
	<span class="l">margin-bottom</span>: 10px;
	<span class="l">position</span>: relative;
	<span class="l">height</span>: 300px;
}
#<span class="l">tabs ul.ui-tabs-nav</span> {
	<span class="l">list-style</span>: none outside none;
	<span class="l">left</span>: 20px;
	<span class="l">top</span>: 20px;
	<span class="l">position</span>: absolute;
	<span class="l">width</span>: 315px;
}
#<span class="l">tabs ul.ui-tabs-nav li</span> {
	<span class="l">border-bottom</span>: 1px solid #bbbbbb;
	<span class="l">height</span>: 85px;
}
#<span class="l">tabs ul.ui-tabs-nav li:last-child</span> { <span class="l">border</span>: none; }
#<span class="l">tabs ul.ui-tabs-nav li a</span> {
	<span class="l">display</span>: block;
	<span class="l">padding</span>: 10px;
	<span class="l">height</span>: 65px;
	<span class="l">line-height</span>: 22px;
}
#<span class="l">tabs ul.ui-tabs-nav li.ui-tabs-selected a</span> { <span class="l">background-color</span>: #697f9a; <span class="l">color</span>: #FFFFFF; }
#<span class="l">tabs ul.ui-tabs-nav li.ui-tabs-selected a:hover</span> { <span class="l">background-color</span>: #697f9a; <span class="l">color</span>: #FFFFFF; }
#<span class="l">tabs ul.ui-tabs-nav li a:hover</span> { <span class="l">background-color</span>: #e3e6e8; }

#<span class="l">tabs .ui-tabs-panel</span> {
	<span class="l">height</span>: 260px;
	<span class="l">position</span>: absolute;
	<span class="l">right</span>: 20px;
	<span class="l">top</span>: 20px;
	<span class="l">width</span>: 400px;
}
</pre>
</div>

<div class="wp-caption alignleft" style="width: 155px;"><a href="http://www.cappelendesign.no/wp-content/uploads/misc/wp-theme-tutorial/screenshot-template-frontpage.jpg" target="_blank" class="shutterset_set_wptutlesson13"><img title="Screenshot of the front page template" src="http://www.cappelendesign.no/wp-content/uploads/misc/wp-theme-tutorial/thumbs/screenshot-template-frontpage.jpg" alt="" /></a></div>

<p>Finally the page template is ready to be used. Edit or add a page and assign the template &#8220;Frontpage&#8221; to it. Add the text you want displayed below the featured posts and preview the page.</p>

<p>You may have noticed I&#8217;ve added styling for a <code>&lt;span&gt;</code> or <code>&lt;p&gt;</code> with the class <code>fullwidth-column</code> in the CSS above. If you switch to HTML view in the editor, you can add some HTML elements with those classes around the text to make the text divide into two columns. There are several solutions to modify the layout of the text written in the editor, but that&#8217;s beyond this tutorial. This was just an example of making the page more pleasing to look at.</p>

<p>If you want to use this page as the front page of your WordPress site, you need to do some settings in WordPress. Go to <code>WordPress admin &raquo; Settings &raquo; Reading</code>, and choose &#8220;A static page&#8221; at &#8220;Front page displays&#8221;, then choose your page in the dropdown-list. You&#8217;ll need another page for the normal loop of recent posts. If you save and refresh your front page, it should display the page template you&#8217;ve just made. But I strongly advise adding more to this page template, e.g. a menu or sidebar, as it totally lacks navigation to the rest of the site!</p>

<h4>WordPress Codex Function references:</h4>
<ul>
<li><a href="http://codex.wordpress.org/Function_Reference/add_image_size" target="_blank" title="Wordpress Function Reference: add_image_size">add_image_size</a></li>
<li><a href="http://codex.wordpress.org/Function_Reference/wp_enqueue_script" target="_blank" title="Wordpress Function Reference: wp_enqueue_script">wp_enqueue_script</a></li>
<li><a href="http://codex.wordpress.org/Function_Reference/get_header" target="_blank" title="Wordpress Function Reference: get_header">get_header</a> and <a href="http://codex.wordpress.org/Function_Reference/get_footer" target="_blank" title="Wordpress Function Reference: get_footer">get_footer</a></li>
<li><a href="http://codex.wordpress.org/Class_Reference/WP_Query" target="_blank" title="Wordpress Class Reference: WP_Query">WP_Query</a></li>
<li><a href="http://codex.wordpress.org/Function_Reference/the_title" target="_blank" title="Wordpress Function Reference: the_title">the_title</a>, <a href="http://codex.wordpress.org/Function_Reference/the_excerpt" target="_blank" title="Wordpress Function Reference: the_excerpt">the_excerpt</a>, and <a href="http://codex.wordpress.org/Function_Reference/the_content" target="_blank" title="Wordpress Function Reference: the_content">the_content</a></li></li>
<li><a href="http://codex.wordpress.org/Function_Reference/the_post_thumbnail" target="_blank" title="Wordpress Function Reference: has_post_thumbnail">has_post_thumbnail</a>, and <a href="http://codex.wordpress.org/Function_Reference/the_post_thumbnail" target="_blank" title="Wordpress Function Reference: the_post_thumbnail">the_post_thumbnail</a></li>
<li><a href="http://codex.wordpress.org/Function_Reference/have_posts" target="_blank" title="Wordpress Function Reference: have_posts">have_posts</a>, and <a href="http://codex.wordpress.org/Function_Reference/the_posts" target="_blank" title="Wordpress Function Reference: the_posts">the_posts</a></li>
</ul>

<p><em>This concludes Lesson 13 of the <a href="http://cappelendesign.no/?p=1229" title="WordPress Theme Tutorial in 14 Lessons">WordPress Theme Tutorial</a>. Go to the next lesson, <a href="http://cappelendesign.no/?p=2888" title="Lesson 14: Finalizing Your Theme">Finalizing Your Theme</a>, or back to the tutorial&#8217;s <a href="http://cappelendesign.no/?p=1229#toc" title="WordPress Theme Tutorial in 14 Lessons Table of Contents">Table of Contents</a>.</em></p>]]></content:encoded>
			<wfw:commentRss>http://cappelendesign.no/2012/01/wordpress-theme-lesson-13-page-templates/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>WordPress Theme Lesson 12: Featured Posts, Dealing with Multiple Loops</title>
		<link>http://cappelendesign.no/2012/01/wordpress-theme-lesson-12-featured-posts-dealing-with-multiple-loops/</link>
		<comments>http://cappelendesign.no/2012/01/wordpress-theme-lesson-12-featured-posts-dealing-with-multiple-loops/#comments</comments>
		<pubDate>Sat, 14 Jan 2012 12:45:08 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Web Coding]]></category>
		<category><![CDATA[wordpress]]></category>
		<category><![CDATA[wordpress theme tutorial]]></category>

		<guid isPermaLink="false">http://www.acornartwork.com/blog/?p=2711</guid>
		<description><![CDATA[In this lesson we&#8217;ll add the featured posts section formed as a jQuery slider on top of the front page. By placing it on the front page template we need to adress the issue of creating multiple loops on the one and same page. This lesson accumulates many of the lessons so far in this [...]]]></description>
			<content:encoded><![CDATA[<div class="wp-caption alignleft"><img title="Wordpress theme tutorial" src="http://www.cappelendesign.no/wp-content/uploads/misc/wp-theme-tutorial/Wordpress_tutorial.png" alt="" /></div>
<p>In this lesson we&#8217;ll add the featured posts section formed as a jQuery slider on top of the front page. By placing it on the front page template we need to adress the issue of creating multiple loops on the one and same page. This lesson accumulates many of the lessons so far in this tutorial; we need to write a custom query code, fetch post thumbnails in a specific image size, and include libraries and scripts to make our slider work.</p>
<p>We begin with registering and enqueuing the necessary scripts for our jQuery slider, for which we will use the <a href="http://flowplayer.org/tools/index.html" target="_blank">jQuery Tools</a> library. We will then add the initializing slider function call in our <code>&lt;head&gt;</code>, and finally write the custom query loop in our <code>index.php</code> to fetch posts from our featured posts category.</p>
<span id="more-2711"></span>

<p>There are many jQuery-based slider plugins you can use (see for instance <a href="http://webdesignledger.com/tutorials/13-super-useful-jquery-content-slider-scripts-and-tutorials" target="_blank" title="Webdesignledger: 13 Super Useful jQuery Content Slider Scripts and Tutorials">13 excellent jQuery sliders</a>), or you can even write your own based on the jQuery library. I chose to use <a href="http://flowplayer.org/tools/index.html" target="_blank">jQuery Tools</a>, a bundle of jQuery plugins where we are to use Scrollable and Navigator. Read through the <a href="http://flowplayer.org/tools/demos/scrollable/plugins/index.html" target="_blank">guides and demos</a> in case you want to read up on how our slider works.</p>

<h3>Registering and Enqueuing Scripts</h3>
<p><a href="http://cappelendesign.no/?p=1962" target="_blank" title="WordPress Theme Lesson 11: Including Javascripts the Right Way">In the previous lesson</a> we learned how to include javascript libraries in WordPress the right way. jQuery Tools is not one of WordPress&#8217;s included libraries, so we need to register it providing its CDN path before we enqueue it. Keep in mind that we need the full library since Navigator isn&#8217;t included in the <code>min</code> bundle. The jQuery Tools depends on jQuery core library being loaded first.</p>

<p>We open up our <code>functions.php</code> and write a PHP function hooked onto the <code>init</code> hook:</p>
<h4 style="font-size:13px;clear:both;">functions.php:</h4>
<div class="wp_syntax">
<pre class="php">
function <span class="l">wpthemetutorial_add_js</span>() {
	<span class="l">if</span> (!<span class="l">is_admin</span>()) {
		<span class="l">wp_register_script</span>(<span class="r">'jquery-tools'</span>, <span class="r">'http://cdn.jquerytools.org/1.2.5/all/jquery.tools.min.js'</span>, <span class="l">array</span>(<span class="r">'jquery'</span>));
		<span class="l">wp_enqueue_script</span>(<span class="r">'jquery-tools'</span>);
	}
}
<span class="l">add_action</span>(<span class="r">'init'</span>, <span class="r">'wpthemetutorial_add_js'</span>);
</pre>
</div>
<p>If you refresh your site and take a look at the source code, the jQuery and jQuery Tools should be included, either where you put the <code>wp_head()</code> or the <code>wp_footer()</code>. Great! Up next is to initialize the slider in a jQuery document ready function.</p>
<h4>WordPress Codex Function references:</h4>
<ul>
<li><a href="http://codex.wordpress.org/Function_Reference/is_admin" target="_blank" title="Wordpress Function Reference: is_admin">is_admin</a></li>
<li><a href="http://codex.wordpress.org/Function_Reference/wp_register_script" target="_blank" title="Wordpress Function Reference: wp_register_script">wp_register_script</a></li>
<li><a href="http://codex.wordpress.org/Function_Reference/wp_enqueue_script" target="_blank" title="Wordpress Function Reference: wp_enqueue_script">wp_enqueue_script</a></li>
<li><a href="http://codex.wordpress.org/Function_Reference/add_action" target="_blank" title="Wordpress Function Reference: add_action">add_action</a></li>
</ul>

<h3>Initializing the Slider Script</h3>
<p>In our <code>&lt;head&gt;</code> section we need to add only one javascript function call, <code>scrollable()</code>, inside a jQuery document ready function, but obviously we need it to be run <em>after</em> the jQuery libraries have been loaded. As shown in the <a href="http://cappelendesign.no/?p=1962" target="_blank" title="WordPress Theme Lesson 11: Including Javascripts the Right Way">previous lesson</a> we hook onto the <code>wp_head</code> hook, which ensures that the jQuery libraries have been loaded first because they were hooked onto the much earlier <code>init</code> hook. Also remember that WordPress loads jQuery in <a href="http://docs.jquery.com/Using_jQuery_with_Other_Libraries" target="_blank" title="jQuery noConflict documentation">no conflict mode</a>.</p>

<p>In our <code>functions.php</code> we add another PHP function which hooks onto the <code>wp_head</code> hook:</p>
<h4 style="font-size:13px;clear:both;">functions.php:</h4>
<div class="wp_syntax">
<pre class="php">
function <span class="l">wpthemetutorial_add_init_js</span>() {
	?&gt;
	&lt;script <span class="b">type</span>=<span class="r">"text/javascript"</span>&gt;
		jQuery.noConflict();
		jQuery(document).ready(function($) {
			jQuery(<span class="r">".scrollable"</span>).scrollable().navigator();
		});
	&lt;/script&gt;
	&lt;?php
}
<span class="l">add_action</span>(<span class="r">'wp_head'</span>, <span class="r">'wpthemetutorial_add_init_js'</span>);
</pre>
</div>
<p>If you refresh your site now, you should see this piece of javascript right at the end of the <code>&lt;head&gt;</code> part. It initialize Scrollable on all elements with the class <code>scrollable</code> as well as Navigator which handles our slider&#8217;s navigation buttons. But before we add the HTML elements to form the slider, I need to explain how we deal with multiple loops in the one and same page.</p>
<h4>WordPress Codex Function references:</h4>
<ul>
<li><a href="http://codex.wordpress.org/Function_Reference/add_action" target="_blank" title="Wordpress Function Reference: add_action">add_action</a></li>
</ul>

<h3>Dealing with Multiple Loops</h3>
<p>What we want is to create two separate loops where each fetches different posts. The problem with multiple loops on the same page is the global core <code>$wp_query</code> value. It is loaded in the blog header and is fed arguments from e.g. <code>GET</code>, creating an array of posts. All loops use the <code>$wp_query</code> object, even though we don&#8217;t refer to this by name. The loop functions <code>have_posts()</code> and <code>the_post()</code> are just convenience wrappers &#8211; they are identical to <code>$wp_query->have_posts()</code> and <code>$wp_query->the_post()</code>, respectively.</p>

<p>The thing is, <code>$wp_query</code> can only be called once. One way to get around this is re-using the query by calling the WordPress function <code>rewind_posts()</code>, or we could simply create a new query object instead of using <code>$wp_query</code>. I will use the last method, creating a new query object for our featured posts so that we can use the <code>$wp_query</code> for our normal loop of posts. This is less work as we don&#8217;t need to alter the code we already have in our <code>index.php</code>. The WordPress Codex <a href="http://codex.wordpress.org/The_Loop#Multiple_Loops" target="_blank" title="Wordpress Codex on Multiple Loops">explains and shows different examples</a> of handling multiple loops if you want to learn more about this topic.</p>

<p>We also want to prevent showing the same post in both the featured posts section and the normal loop below. If the writer choose to mark a very recent publised post as featured, we may risk showing the same post twice in the same screen. To solve this we store an array of post IDs when we loop through featured posts, and in our original loop we add a line of code which skips the current post if its ID is in our array.</p>

<h3>Custom query for Fetching Featured Posts</h3>
<p>Using the method explained above we don&#8217;t need to alter our existing loop in our <code>index.php</code>, except adding the one line to prevent duplicate posts. Let&#8217;s first add the custom loop. Just above and before the original loop we create a new query object and write a loop using this object, but otherwise the procedure is the same as a normal loop.</p>
<p>In our custom query we want to fetch 9 posts from the category named &#8216;Featured&#8217; (replace the name if you chose another category name), so we get three on each slide.</p>
<p>We also need to add some extra HTML to make our jQuery slider work, such as navigation elements and dividing the posts three by three in each slide/div element. In our <code>index.php</code>, add this piece of code <em>before</em> the original loop, but right <em>after</em> <code>&lt;div class="content"&gt;</code>:</p>
<h4 style="font-size:13px;clear:both;">index.php:</h4>
<div class="wp_syntax">
<pre class="php">
&lt;div <span class="b">id</span>=<span class="r">"featured-scrollable"</span>&gt;
	&lt;div <span class="b">class</span>=<span class="r">"scrollable"</span>&gt;
		&lt;h3&gt;Featured Posts&lt;/h3&gt;
		&lt;div <span class="b">class</span>=<span class="r">"navi"</span>&gt;&lt;/div&gt;
		&lt;div <span class="b">class</span>=<span class="r">"items"</span>&gt;
			<span class="y">&lt;?php <span class="b">$counter</span> = 0; <span class="b">$perslide</span> = 3; ?&gt;</span>
			&lt;div <span class="b">class</span>=<span class="r">"scrollable-item"</span>&gt;
			<span class="y">&lt;?php <span class="b">$my_query</span> = new <span class="l">WP_Query</span>(<span class="r">'category_name=Featured&#038;posts_per_page=9'</span>);</span>
			<span class="y"><span class="l">while</span> (<span class="b">$my_query->have_posts()</span>) : <span class="b">$my_query->the_post</span>();</span>
				<span class="y"><span class="b">$do_not_duplicate</span>[] = <span class="b">$post->ID</span>;</span>
				<span class="y"><span class="l">if</span> (<span class="b">$counter</span>++ &gt; <span class="b">$perslide</span>) { <span class="b">$counter</span> = 1; ?&gt;</span>&lt;/div&gt;&lt;div <span class="b">class</span>=<span class="r">"scrollable-item"</span>&gt;<span class="y">&lt;?php } ?&gt;</span>
				&lt;div <span class="b">class</span>=<span class="r">"item"</span>&gt;
					<span class="y">&lt;?php <span class="l">if</span> (<span class="l">has_post_thumbnail</span>()) : <span class="l">the_post_thumbnail</span>(<span class="r">'featuredposts-thumbnail'</span>); <span class="l">endif</span>; ?&gt;</span>
					&lt;h2&gt;&lt;a <span class="b">href</span>=<span class="r">"</span><span class="y">&lt;?php <span class="l">the_permalink</span>(); ?&gt;</span><span class="r">"</span> <span class="b">rel</span>=<span class="r">"bookmark"</span> <span class="b">title</span>=<span class="r">"</span><span class="y">&lt;?php <span class="l">the_title</span>(); ?&gt;</span><span class="r">"</span>&gt;<span class="y">&lt;?php <span class="l">the_title</span>(); ?&gt;</span>&lt;/a&gt;&lt;/h2&gt;
				&lt;/div&gt;
				<span class="y">&lt;?php <span class="l">endwhile</span>; ?&gt;</span>
			&lt;/div&gt;
		&lt;/div&gt;		
	&lt;/div&gt;
	&lt;div <span class="b">class</span>=<span class="r">"featured-scrollable-nav"</span>&gt;
		&lt;a <span class="b">class</span>=<span class="r">"featured prev left"</span>&gt;&lt;/a&gt;
		&lt;a <span class="b">class</span>=<span class="r">"featured next right"</span>&gt;&lt;/a&gt;
	&lt;/div&gt;
	&lt;div <span class="b">style</span>=<span class="r">"clear:both;"</span>&gt;&lt;/div&gt;
&lt;/div&gt;
</pre>
</div>
<p>If you look closely at the loop you&#8217;ll see we create a new <code>WP_Query</code> object and stores it in <code>$my_query</code>. We then refer to this object when calling <code>have_posts()</code> and <code>the_post()</code>. At this point we&#8217;ve created a loop without touching the global <code>$wp_query</code> object. As arguments to <code>WP_Query</code> we add the category name and the number of posts we want to fetch. Then for each post we store the ID in the array <code>$do_not_duplicate</code>, which we will later use in our original loop to prevent duplicates.</p>

<p>The values <code>$counter</code>, <code>$perslide</code> and the line inside the loop immidiately before <code>&lt;div class="item"&gt;</code> makes sure we loop through three posts before we close the <code>&lt;div&gt;</code> and start a new <code>&lt;div class="scrollable-item"&gt;</code>.</p>

<p>Remember from <a href="http://cappelendesign.no/?p=1654" target="_blank" title="WordPress Theme Lesson 8: Post Thumbnails">Lesson 8</a> where we defined an image size specific for our featured posts section? We&#8217;ve put the shorthand name into use, <code>featuredposts-thumbnail</code>, as argument when we fetch the post thumbnail, giving us just the right size we need to fit three posts nicely next to each other.</p>

<h3>Prevent Duplicates</h3>
<p>We need to do one more thing before we&#8217;re done writing the featured posts section, which is to add a line in our original loop below to prevent duplicates. We created an array <code>$do_not_duplicate</code> looping through our featured posts, and we need to make sure no posts with any of these IDs are repeated in the normal loop. Scroll further down <code>index.php</code> and find our original loop line:</p>
<div class="wp_syntax">
<pre class="php">
<span class="l">if</span> (<span class="l">have_posts</span>()) : <span class="l">while</span> (<span class="l">have_posts</span>()) : <span class="l">the_post</span>();
</pre>
</div>
<p>Make a <em>new line below this</em>, and write:</p>
<div class="wp_syntax">
<pre class="php">
<span class="l">if</span> (<span class="l">in_array</span>(<span class="b">$post->ID</span>, <span class="b">$do_not_duplicate</span>)) <span class="l">continue</span>;
</pre>
</div>
<p>This checks whether or not the current post ID resides in the array <code>$do_not_duplicate</code>, and if it does, it continues to the next post, skipping the one we were at.</p>

<p>Our slider will not be visible or work before we add styling to it. Let&#8217;s do that!</p>

<h4>WordPress Codex Function references:</h4>
<ul>
<li><a href="http://codex.wordpress.org/Class_Reference/WP_Query" target="_blank" title="Wordpress Class Reference: WP_Query">WP_Query</a></li>
<li><a href="http://codex.wordpress.org/Function_Reference/has_post_thumbnail" target="_blank" title="Wordpress Function Reference: has_post_thumbnail">has_post_thumbnail</a></li>
<li><a href="http://codex.wordpress.org/Function_Reference/the_post_thumbnail" target="_blank" title="Wordpress Function Reference: the_post_thumbnail">the_post_thumbnail</a></li>
<li><a href="http://codex.wordpress.org/Function_Reference/the_permalink" target="_blank" title="Wordpress Function Reference: the_permalink">the_permalink</a></li>
<li><a href="http://codex.wordpress.org/Function_Reference/the_title" target="_blank" title="Wordpress Function Reference: the_title">the_title</a></li>
</ul>

<h3>Styling our Featured Posts Slider</h3>
<p>I&#8217;ve copied most of my CSS from the <a href="http://flowplayer.org/tools/demos/scrollable/plugins/index.html" target="_blank">guide</a> provided by jQuery Tools&#8217;s website and then modified it to my needs. In your <code>style.css</code> add:</p>
<h4 style="font-size:13px;clear:both;">style.css:</h4>
<div class="wp_syntax" style="height:350px;overflow:auto;">
<pre class="css">
<span class="g">/* SCROLLABLE FEATURED */</span>
.<span class="l">scrollable</span> {
	<span class="l">position</span>: relative;
	<span class="l">overflow</span>: hidden;
	<span class="l">width</span>: 490px;
	<span class="l">height</span>: 185px;
}
.<span class="l">scrollable .items</span> {
	<span class="l">width</span>: 2000em; <span class="g">/* Must be large, but not too large */</span>
	<span class="l">position</span>: absolute;
	<span class="l">clear</span>: both;
}
.<span class="l">items div.scrollable-item</span> { 
	<span class="l">float</span>: left; 
	<span class="l">width</span>: 490px; 
	<span class="l">margin-top</span>: 8px; 
}

#<span class="l">featured-scrollable</span> {
	<span class="l">background-color</span>: #ffffff;
	<span class="l">border</span>: 1px solid #dadcde; 
	<span class="l">padding</span>: 15px;
	<span class="l">margin-bottom</span>: 20px;
}
#<span class="l">featured-scrollable h3</span> {
	<span class="l">font-family</span>: Arial, Verdana, sans-serif;
	<span class="l">font-size</span>: 18px;
	<span class="l">color</span>: #4f5051;
	<span class="l">float</span>: left;
}
#<span class="l">featured-scrollable .items div.scrollable-item .item</span> { <span class="l">float</span>: left; <span class="l">width</span>: 140px; <span class="l">margin</span>: 0 11px; height: 155px; }
#<span class="l">featured-scrollable .items div.scrollable-item .item img</span> { <span class="l">border</span>: 1px solid #71869f; }
#<span class="l">featured-scrollable .items div.scrollable-item .scrollable-postthumbnail</span> { <span class="l">width</span>: 140px; }
#<span class="l">featured-scrollable .items div.scrollable-item .item h2</span> { <span class="l">padding-top</span>: 4px; <span class="l">font-size</span>: 13px; }

#<span class="l">featured-scrollable .featured-scrollable-nav</span> { 
	<span class="l">display</span>: block; 
	<span class="l">width</span>: 100px;
	<span class="l">margin</span>: 15px auto 0 auto; 
	<span class="l">padding-left</span>: 35px;
}
#<span class="l">featured-scrollable .featured-scrollable-nav a</span> { 
	<span class="l">cursor</span>: pointer; 
	<span class="l">margin-right</span>: 10px;
	<span class="l">background</span>: url(<span class="r">"images/slider-arrows.png"</span>) no-repeat 0 0;
	<span class="l">font-size</span>: 1px;
	<span class="l">display</span>: block; 
	<span class="l">height</span>: 30px;
	<span class="l">width</span>: 30px;
	<span class="l">float</span>: left;
}
#<span class="l">featured-scrollable .featured-scrollable-nav a.right</span> { <span class="l">background-position</span>: 0 -30px; <span class="l">margin-right</span>: 0; }
#<span class="l">featured-scrollable .featured-scrollable-nav a.disabled</span> { <span class="l">visibility</span>: hidden; }
#<span class="l">featured-scrollable .featured-scrollable-nav a.left:hover</span> { <span class="l">background-position</span>: -30px 0; }
#<span class="l">featured-scrollable .featured-scrollable-nav a.right:hover</span> { <span class="l">background-position</span>: -30px -30px; }

.<span class="l">navi</span> {
	<span class="l">margin-left</span>: 440px;
	<span class="l">margin-top</span>: 5px;
	<span class="l">width</span>: 200px;
	<span class="l">height</span>: 20px;
}
.<span class="l">navi a</span> {
	<span class="l">width</span>: 8px;
	<span class="l">height</span>: 8px;
	<span class="l">float</span>: left;
	<span class="l">margin</span>: 3px;
	<span class="l">background</span>: url(<span class="r">"images/navigator.png"</span>) 0 0 no-repeat;
	<span class="l">display</span>: block;
	<span class="l">font-size</span>: 1px;
}
.<span class="l">navi a:hover</span> { <span class="l">background-position</span>: 0 -8px; }
.<span class="l">navi a.active</span> { <span class="l">background-position</span>: 0 -16px; }
</pre>
</div>
<div class="wp-caption alignleft" style="width: 160px; margin-top: 10px;"><a href="http://www.cappelendesign.no/wp-content/uploads/misc/wp-theme-tutorial/screenshot-featuredposts.jpg" target="_blank" class="shutterset_set_wptutlesson11"><img title="Our featured posts slider" src="http://www.cappelendesign.no/wp-content/uploads/misc/wp-theme-tutorial/thumbs/screenshot-featuredposts.jpg" alt="" /></a><p class="wp-caption-text">Our featured posts slider</p></div>
<p><a href="http://www.cappelendesign.no/wp-content/uploads/misc/wp-theme-tutorial/images-lesson-12.zip" title="Download images">Click here</a> to download the images used in this CSS code, unzip and put the images into the <code>images</code> folder in your theme folder.</p>

<p>Refresh the front page and see that the slider works. Three posts from your chosen category are displayed with the post thumbnail and title, three on each slide. The right and left buttons are not displayed if there are no more posts in the given direction. We also have working navigation &#8220;dots&#8221; in the upper right hand corner. And if you flip through your posts you should also not see any duplicates.</p>

<p><em>This concludes Lesson 12 of the <a href="http://cappelendesign.no/?p=1229" title="WordPress Theme Tutorial in 14 Lessons">WordPress Theme Tutorial</a>. Go to the next lesson, <a href="http://cappelendesign.no/?p=1896" title="Lesson 13: Page Templates">Page Templates</a>, or back to the tutorial&#8217;s <a href="http://cappelendesign.no/?p=1229#toc" title="WordPress Theme Tutorial in 14 Lessons Table of Contents">Table of Contents</a>.</em></p>]]></content:encoded>
			<wfw:commentRss>http://cappelendesign.no/2012/01/wordpress-theme-lesson-12-featured-posts-dealing-with-multiple-loops/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>WordPress Theme Lesson 11: Including Javascripts The Right Way</title>
		<link>http://cappelendesign.no/2012/01/wordpress-theme-lesson-11-including-javascripts-the-right-way/</link>
		<comments>http://cappelendesign.no/2012/01/wordpress-theme-lesson-11-including-javascripts-the-right-way/#comments</comments>
		<pubDate>Sat, 14 Jan 2012 12:00:43 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Web Coding]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jquery]]></category>
		<category><![CDATA[wordpress]]></category>
		<category><![CDATA[wordpress theme tutorial]]></category>

		<guid isPermaLink="false">http://www.acornartwork.com/blog/?p=1962</guid>
		<description><![CDATA[Our design doesn&#8217;t require much javascript, but it&#8217;s increasingly more common to use javascript to spice up a web design or for displaying dynamic content. For our theme we need to use jQuery for dynamic content on top of the front page, a slider with featured posts. You can improve more of the theme by [...]]]></description>
			<content:encoded><![CDATA[<div class="wp-caption alignleft"><img title="Wordpress theme tutorial" src="http://www.cappelendesign.no/wp-content/uploads/misc/wp-theme-tutorial/Wordpress_tutorial.png" alt="" /></div>
<p>Our design doesn&#8217;t require much javascript, but it&#8217;s increasingly more common to use javascript to spice up a web design or for displaying dynamic content. For our theme we need to use jQuery for dynamic content on top of the front page, a slider with featured posts. You can improve more of the theme by using <a href="http://jqueryui.com/demos/tabs/" target="_blank" title="jQuery Tabs demo">jQuery tabs</a>, <a href="http://www.1stwebdesigner.com/freebies/jquery-accordion-menus-tutorials/" target="_blank" title="A list of jQuery Accordion libraries">accordion menus</a>, or to load content without requiring a page refresh. It can make the website more elegant, as long as you don&#8217;t overdo it.</p>

<p>If you&#8217;re creating a web page from scratch without a CMS, you&#8217;d just add each javascript library or script in the <code>&lt;head&gt;</code> section, like we&#8217;ve done with the stylesheet so far in this theme. But using WordPress as a CMS we need to take in consideration all scripts which might be added in the <code>wp_head</code>, scripts from both WordPress itself and all active plugins. This is why we need a safe way of adding scripts so that we don&#8217;t load the same libraries more than once, and that WordPress loads them in the right order. WordPress has a quite-simple-to-use mechanism for this.</p>
<span id="more-1962"></span>
<p>Managing javascripts in WordPress is referred to as <em>registering</em> and <em>enqueuing</em> scripts, which is hooked onto a initializing state of WordPress. We register a script if it&#8217;s unknown to WordPress and enqueue it, which puts it in WordPress&#8217; queue of scripts to load in either <code>wp_head</code> or <code>wp_footer</code> (<a href="http://cappelendesign.no/?p=2217" target="_blank" title="WordPress Theme Lesson 2: Creating Files and Activating the Theme">remember wp_head and wp_footer from Lesson 2?</a>). WordPress also takes care of preventing loading the same script twice. Let&#8217;s say some plugin and our theme loads the same library (e.g. jQuery UI), which means we have two enqueue-calls with the same library but the script will only be loaded once. If we didn&#8217;t use WordPress&#8217; queue of scripts, it&#8217;s easy to risk loading a script twice, and most likely cause both scripts to stop functioning. We can also de-register a script, preventing it from being enqueued and included at all.</p>

<p>Included with WordPress there&#8217;s a bunch of scripts, such as the core jQuery script and jQuery&#8217;s plugins. None of them are loaded as default, but we can choose which to load and when, without having a copy of the script files in our theme folder. See a list of all scripts included with WordPress <a href="http://codex.wordpress.org/Function_Reference/wp_enqueue_script#Default_scripts_included_with_WordPress" target="_blank" title="Wordpress Function reference: Default scripts included with WordPress">here</a>. The most interesting of them all is <code>jquery</code>, <code>jquery-ui-core</code>, <code>scriptaculous</code>, <code>thickbox</code> and <code>tiny_mce</code> (Tiny MCE is the text editor used in WordPress). Keep in mind that with libraries included in WordPress we only need to enqueue, not register them. It&#8217;s when we add our own script files we need to register them first, because they are unknown to WordPress (WordPress doesn&#8217;t know where it can find the files and what dependencies the script has).</p>

<p><strong>An important note on jQuery</strong>: Keep in mind that WordPress loads jQuery in <a href="http://docs.jquery.com/Using_jQuery_with_Other_Libraries" target="_blank" title="jQuery noConflict documentation">no conflict mode</a>. What this means is that in your scripts you must use <code>jQuery</code> instead of the <code>$</code> sign unless you assign jQuery into another variable.</p>

<p><strong>NB</strong>:This whole lesson is code samples showing how we include scripts in WordPress and <strong>not something we are to do in our theme</strong>. We return to our theme in the <a href="http://cappelendesign.no/?p=2711" target="_blank" title="WordPress Theme Lesson 12: Featured Posts, Dealing with Multiple Loops">next lesson</a>, where we will use what we learned here.</p>

<h3>Enqueuing WordPress-included Scripts</h3>
<p>We do all the registering and enqueuing scripts in a function hooked onto the WordPress hook <code>init</code>. Hooking onto any other hook than <code>init</code> wil cause issues to arise.</p>

<p>We enqueue scripts using the WordPress function <code>wp_enqueue_script()</code>, with the script&#8217;s shorthand name defined by WordPress (the names are listed in <a href="http://codex.wordpress.org/Function_Reference/wp_enqueue_script#Default_scripts_included_with_WordPress" target="_blank" title="Wordpress Function reference: Default scripts included with WordPress">the full list</a>) as the first argument. The second argument is the file&#8217;s location, which we don&#8217;t need to provide with WordPress included scripts so we use <code>null</code>. The third argument is the script&#8217;s dependencies. Here we can tell which scripts we need to be loaded <em>before</em> this script can be loaded. For instance, if we want to load the jQuery UI library, we need to load the core jQuery library first, so we put jQuery as a dependency. Note that we don&#8217;t need to enqueue the jQuery core library if we&#8217;ve added it as a dependency. WordPress will automatically enqueue all scripts in the list of dependencies.</p>

<p>Let&#8217;s say we want to use the jQuery Tabs library, one of the scripts included in WordPress. It depends on the jQuery UI, which in turn depends on the core jQuery library. To do this, we only enqueue the jQuery Tabs using the shorthand name <code>jquery-ui-tabs</code>, and set <code>jquery-ui-core</code> and <code>jquery</code> as dependencies. We put the <code>wp_enqueue_script()</code> in a PHP function in our <code>functions.php</code> and hooks it into the <code>init</code> hook:</p>
<h4 style="font-size:13px;clear:both;">functions.php:</h4>
<div class="wp_syntax">
<pre class="php">
function <span class="l">wpthemetutorial_add_js</span>() {
	<span class="l">if</span> (!<span class="l">is_admin</span>()) {
		<span class="l">wp_enqueue_script</span>(<span class="r">'jquery-ui-tabs'</span>, null, <span class="l">array</span>(<span class="r">'jquery'</span>, <span class="r">'jquery-ui-core'</span>));
	}
}
<span class="l">add_action</span>(<span class="r">'init'</span>, <span class="r">'wpthemetutorial_add_js'</span>);
</pre>
</div>
<p>If you try this function and refresh and take a look on the source code of your site, you should see that jQuery, jQuery UI and the jQuery UI Tabs libraries are included. They normally resides in the footer where you put your <code>wp_footer</code>, and it will do so even if you add a False to the <code>in_footer</code> parameter. Sometimes they are being loaded in the <code>wp_head</code> hook in the <code>&lt;head&gt;</code> section. It&#8217;s really up to WordPress where the scripts are loaded.</p>

<p>You&#8217;ve probably noticed the if-check on <code>is_admin()</code>. With conditional tags we can choose which pages we want to enqueue scripts, preventing loading unecessary scripts where we don&#8217;t need them &#8211; in this case the WordPress admin interface. You could e.g. enqueue scripts only at a particular page template, or anywhere except the front page. To learn more take a look at the <a href="http://codex.wordpress.org/Conditional_Tags" target="_blank" title="WordPress Codex on Conditional tags">list of conditional tags</a>.</p>

<p>And that&#8217;s how you enqueue a WordPress-included script. But what if you want to remove a WordPress-included script, and replace it with another file, such as an older or newer version?</p>

<h3>Deregistering a WordPress-included Script</h3>
<p>If you want to prevent a script to be included, or provide another file or URL to a WordPress-included script, you can deregister it with <code>wp_deregister_script()</code>. It&#8217;s used in the same way as <code>wp_enqueue_script()</code>, providing the shorthand name as the first parameter.</p>
<p>Perhaps a more common example of deregistering a WordPress-included script is when you of some reason want to use the CDN copy of jQuery instead of the one included with WordPress. In that case we deregister the jQuery script, register it again with the CDN URL, and finally enqueue it.</p>
<h4 style="font-size:13px;clear:both;">functions.php:</h4>
<div class="wp_syntax">
<pre class="php">
function <span class="l">wpthemetutorial_add_js</span>() {
	<span class="l">if</span> (!<span class="l">is_admin</span>()) {
		<span class="l">wp_deregister_script</span>(<span class="r">'jquery'</span>);
		<span class="l">wp_register_script</span>(<span class="r">'jquery'</span>, <span class="r">'http://ajax.googleapis.com/ajax/libs/jquery/1.4/jquery.min.js'</span>);
		<span class="l">wp_enqueue_script</span>(<span class="r">'jquery'</span>);
	}
}
<span class="l">add_action</span>(<span class="r">'init'</span>, <span class="r">'wpthemetutorial_add_js'</span>);
</pre>
</div>
<p>Note that we&#8217;ve registered and enqueued it with the same shorthand name, <code>jquery</code>, making sure all dependencies on this library works normally.</p>

<h3>Registering and Enqueuing Your Own Scripts Files</h3>
<p>Now that we&#8217;ve been through WordPress-included scripts, let&#8217;s learn how we can include our own scripts. It could be a jQuery plugin not included in WordPress which we&#8217;ve copied into our theme folder, or a self-written script. In this case we must remember to register them before we enqueue it, so WordPress knows where to find the file. By registering a script we make up an unique shorthand name which we will then use when we enqueue it.</p>
<p>In this example we&#8217;ll include a script called <code>wpthemetutorial.js</code> which requires the jQuery core library being loaded first. It resides in a folder called <code>js</code> in the theme folder.</p>
<h4 style="font-size:13px;clear:both;">functions.php:</h4>
<div class="wp_syntax">
<pre class="php">
function <span class="l">wpthemetutorial_add_js</span>() {
	<span class="l">if</span> (!<span class="l">is_admin</span>()) {
		<span class="l">wp_register_script</span>(<span class="r">'wpthemetutorial_js'</span>, <span class="l">get_bloginfo</span>(<span class="r">'stylesheet_directory'</span>) . <span class="r">'/js/wpthemetutorial.js'</span>, <span class="l">array</span>(<span class="r">'jquery'</span>));
		<span class="l">wp_enqueue_script</span>(<span class="r">'wpthemetutorial_js'</span>);
	}
}
<span class="l">add_action</span>(<span class="r">'init'</span>, <span class="r">'wpthemetutorial_add_js'</span>);
</pre>
</div>
<p>We register the script with the shorthand name <code>wpthemetutorial_js</code> and set the path as second parameter by getting the stylesheet directory and adding the rest of the path after it. The jQuery core library is its only dependency. We then enqueue the script by using the name we gave it when registering. And that&#8217;s it.</p>

<h3>Adding Initializing Script Code in The &lt;head&gt;</h3>
<p>When using jQuery libraries and plugins you often need to initialize functions on document ready, code which you normally&#8217;d place in the <code>&lt;head&gt;</code> section. The safe way to do this with WordPress is ensuring that libraries this code depends on are loaded first, e.g. the jQuery library.</p>

<p>We will hook our function onto the <code>wp_head</code> hook. The hook <code>init</code> which we&#8217;ve registered and enqueued all of our scripts are run before <code>wp_head</code>. This ensures that all libraries are loaded before we add our intializing script.</p>

<p>In the example below we assume we&#8217;ve enqueued the jQuery UI Tabs (the first example above). We need to initialize jQuery Tabs by calling the <code>.tabs()</code> function inside a jQuery document ready. So we create a PHP function which hooks onto the <code>wp_head</code>. Don&#8217;t forget the <code>jQuery.noConflict()</code>.</p>
<h4 style="font-size:13px;clear:both;">functions.php:</h4>
<div class="wp_syntax">
<pre class="php">
<span class="g">/* Adds JS code to be run in &lt;head&gt; */</span>
function <span class="l">wpthemetutorial_add_init_js</span>() {
	?&gt;
	&lt;script <span class="b">type</span>=<span class="r">"text/javascript"</span>&gt;
		jQuery.noConflict();
		jQuery(document).ready(function($) {
			jQuery(<span class="r">"#tabs"</span>).tabs().tabs('<span class="r">rotate'</span>, 5000);
		});
	&lt;/script&gt;
	&lt;?php
}
<span class="l">add_action</span>(<span class="r">'wp_head'</span>, <span class="r">'wpthemetutorial_add_init_js'</span>);
</pre>
</div>

<h3>Back to Our Theme&#8230;</h3>
<p>Hopefully these examples should help you understand the basics and be prepared when we need to add scripts to our theme later in this tutorial. We are going to add a jQuery slider for our featured posts in the next lesson, and for that we need to register and enqueue the CDN of <a href="http://flowplayer.org/tools/index.html" target="_blank" title="jQuery Tools">jQuery Tools</a>, with jQuery core as a dependency. We also also need to initialize the slider in the <code>wp_head</code>. I&#8217;ll show you the code in the next lesson, but I won&#8217;t explain it in depth.</p>
<p>If you tried out the above examples, remember to delete what you might&#8217;ve added in your <code>functions.php</code> in this lesson.</p>

<h4>WordPress Codex Function references:</h4>
<ul>
<li><a href="http://codex.wordpress.org/Function_Reference/wp_enqueue_script" target="_blank" title="Wordpress Function Reference: wp_enqueue_script">wp_enqueue_script</a></li>
<li><a href="http://codex.wordpress.org/Function_Reference/wp_deregister_script" target="_blank" title="Wordpress Function Reference: wp_deregister_script">wp_deregister_script</a></li>
<li><a href="http://codex.wordpress.org/Function_Reference/wp_register_script" target="_blank" title="Wordpress Function Reference: wp_register_script">wp_register_script</a></li>
<li><a href="http://codex.wordpress.org/Function_Reference/add_action" target="_blank" title="Wordpress Function Reference: add_action">add_action</a></li>
<li><a href="http://codex.wordpress.org/Function_Reference/is_admin" target="_blank" title="Wordpress Function Reference: is_admin">is_admin</a></li>
</ul>

<p><em>This concludes Lesson 11 of the <a href="http://cappelendesign.no/?p=1229" title="WordPress Theme Tutorial in 14 Lessons">WordPress Theme Tutorial</a>. Go to the next lesson, <a href="http://cappelendesign.no/?p=2711" title="Lesson 12: Featured Posts, Dealing with Multiple Loops">Featured Posts, Dealing with Multiple Loops</a>, or back to the tutorial&#8217;s <a href="http://cappelendesign.no/?p=1229#toc" title="WordPress Theme Tutorial in 14 Lessons Table of Contents">Table of Contents</a>.</em></p>]]></content:encoded>
			<wfw:commentRss>http://cappelendesign.no/2012/01/wordpress-theme-lesson-11-including-javascripts-the-right-way/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>WordPress Theme Lesson 10: Related Posts, Dealing with Nested Loops</title>
		<link>http://cappelendesign.no/2011/10/wordpress-theme-lesson-10-related-posts-dealing-with-nested-loops/</link>
		<comments>http://cappelendesign.no/2011/10/wordpress-theme-lesson-10-related-posts-dealing-with-nested-loops/#comments</comments>
		<pubDate>Sat, 22 Oct 2011 18:45:22 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Web Coding]]></category>
		<category><![CDATA[wordpress]]></category>
		<category><![CDATA[wordpress theme tutorial]]></category>

		<guid isPermaLink="false">http://www.acornartwork.com/blog/?p=1825</guid>
		<description><![CDATA[In this lesson we&#8217;ll focus on writing the related posts section in the single post view, which should appear between the post content and the comments section. The idea is to list a small number of posts related to the post the reader has just read to inspire her or him to read more of [...]]]></description>
			<content:encoded><![CDATA[<div class="wp-caption alignleft"><img title="Wordpress theme tutorial" src="http://www.cappelendesign.no/wp-content/uploads/misc/wp-theme-tutorial/Wordpress_tutorial.png" alt="" /></div>
<p>In this lesson we&#8217;ll focus on writing the related posts section in the single post view, which should appear between the post content and the comments section. The idea is to list a small number of posts related to the post the reader has just read to inspire her or him to read more of your posts. By adding this piece of code we need to learn more about the Loop and how we handle custom queries.</p>

<p>In all of our templates, including the <code>single.php</code>, we have a Loop which loops through a predefined set of posts. We use a Loop in the <code>single.php</code> and it works just fine because the set of posts we loop through contains a single post. The challenge is not only to write a custom WordPress query, but we need to be extra cautious as this results in nested Loops, a new Loop inside another Loop. We need to make sure that the nested Loop exits properly so the Loop on the outside can continue as normal. There are several ways to make this work, the WordPress Codex <a href="http://codex.wordpress.org/The_Loop#Nested_Loops" target="_blank" title="Wordpress Codex on Nested Loops">suggests one way</a>, but I&#8217;ll show you another method since we&#8217;re going through WordPress&#8217;s suggested way in a later lesson.</p>
<span id="more-1825"></span>

<h4>The Issue With Nested Loops</h4>
<p>In the Loops we&#8217;ve written before we use <code>the_post()</code> to set up the post object so we can use simpler functions getting post information, such as <code>the_title()</code> and <code>the_permalink()</code>. When we want to create a loop inside a loop, stating <code>the_post()</code> in another query will mess up the global post object in the parenting loop, because we change the global post into another post object. In our case in the single post template this results in getting the wrong post comments. WordPress will insert the comments template based on the global post object, which should be the current single post object we&#8217;re displaying, not the last post object in our nested posts query. A simple solution is to store the global post object before we enter the second loop, and set it back again when we&#8217;re done, and finally use a WordPress function to reset the query.</p>

<p>Another method is to use one of WordPress&#8217;s simple query functions, <code>get_posts()</code>, to fetch posts, and use <code>setup_postdata()</code> to set up each post object, a method which doesn&#8217;t interfere with <code>the_post()</code>. With <code>get_posts()</code> you have a large repertoire of arguments to filter and pick out posts. See examples of arguments in the <a href="http://codex.wordpress.org/Function_Reference/get_posts" target="_blank" title="Wordpress Codex on get_posts">WordPress Codex on get_posts</a>, and the <a href="http://codex.wordpress.org/Class_Reference/WP_Query#Parameters" target="_blank" title="Wordpress Codex on parameters to WP_Query">full list of parameters</a> in WordPress Codex outlining the <code>WP_Query()</code> function, which is equivalent to <code>get_posts()</code>.</p>

<a name="relatedposts"></a><h3>Related Posts</h3>
<p>How are we going to find posts that are related to the current post? The simplest solution is to fetch posts in the same category, which will work fine as long as the author actively use categories. It&#8217;s possible to further specialize our search for related posts by looking at e.g. post tags as well, but that requires quite a bit more code. If you&#8217;re interested in fetching related posts via tags, <a href="http://wp.tutsplus.com/tutorials/display-style-related-posts-for-your-wordpress-site/" title="Display &#038; Style Related Posts For Your WordPress Site at WpTuts+" target="_blank">WpTuts+ has an excellent article on this</a>. I&#8217;ll show you the simple solution, looking for posts in the same category.</p>

<p>Our related posts query is going to fetch three posts, which are in one or more of the same category as the current post, sorted by date (the most recent first). Importantly we also want to exclude the current post from the set of related posts.</p>

<p>Remember we left an empty <code>div</code> with the class <code>related-template</code> in our <code>single.php</code>, between the post and comments section? Let&#8217;s write this code in its place:</p>
<h4 style="font-size:13px;clear:both;">single.php:</h4>
<div class="wp_syntax">
<pre class="php">
&lt;div <span class="b">class</span>=<span class="r">"related-template"</span>&gt;
	&lt;h3&gt;Related posts&lt;/h3&gt;
	&lt;ul <span class="b">class</span>=<span class="r">"spaceinvader-list"</span>&gt;
		<span class="y">&lt;?php</span>
		<span class="y"><span class="b">$backup</span> = <span class="b">$post</span>; <span class="g">//Backup current post object</span></span>
		<span class="y"><span class="b">$current</span> = <span class="b">$post-&gt;ID</span>;  <span class="g">//get current post id </span></span>
								
		<span class="y"><span class="l">global</span> <span class="b">$post</span>;</span>

		<span class="y"><span class="g">//Fetch categories of current post</span></span>
		<span class="y"><span class="b">$counter</span> = 0;</span>
		<span class="y"><span class="b">$allcats</span> = <span class="r">''</span>;</span>
		<span class="y"><span class="l">foreach</span> ((<span class="l">get_the_category</span>()) as <span class="b">$cat</span>) {</span>
		<span class="y">	<span class="l">if</span> (<span class="b">$counter</span> > 0) <span class="b">$allcats</span> .= <span class="r">','</span>;</span>
		<span class="y">	<span class="b">$allcats</span> .= <span class="b">$cat->cat_ID</span>;</span>
		<span class="y">	<span class="b">$counter</span>++;</span>
		<span class="y">}</span>

		<span class="y"><span class="g">//Get posts</span></span>
		<span class="y"><span class="b">$myposts</span> = <span class="l">get_posts</span>(<span class="r">'numberposts=3&#038;order=DESC&#038;category='</span>.<span class="b">$currentCat</span>.<span class="r">'&#038;exclude='</span>.<span class="b">$current</span>);</span>
		<span class="y"><span class="l">foreach</span> (<span class="b">$myposts</span> as <span class="b">$post</span>) :</span>
		<span class="y">	<span class="l">setup_postdata</span>(<span class="b">$post</span>);</span>
		<span class="y">	?&gt;</span>
		&lt;li&gt;
			&lt;a <span class="b">href</span>=<span class="r">"</span><span class="y">&lt;?php <span class="l">the_permalink</span>(); ?&gt;</span><span class="r">"</span> <span class="b">title</span>=<span class="r">"</span><span class="y">&lt;?php <span class="l">the_title</span>(); ?&gt;</span><span class="r">"</span> <span class="b">rel</span>=<span class="r">"bookmark"</span>&gt;<span class="y">&lt;?php <span class="l">the_title</span>(); ?&gt;</span>&lt;/a&gt;
			&lt;span <span class="b">class</span>=<span class="r">"related-posts-date"</span>&gt;<span class="y">&lt;?php <span class="l">the_time</span>(<span class="r">'F jS, Y'</span>); ?&gt;</span>&lt;/span&gt;
		&lt;/li&gt;
		<span class="y">&lt;?php <span class="l">endforeach</span>;</span>
								
		<span class="y"><span class="b">$post</span> = <span class="b">$backup</span>; <span class="g">//restore current post object</span></span>
		<span class="y"><span class="l">wp_reset_query</span>();</span>
		<span class="y">?&gt;</span>
	&lt;/ul&gt;
&lt;/div&gt;
</pre>
</div>
<p>The first thing this code does is to store the important global post object in the PHP value <code>$backup</code>, and we also store the current post ID so we can exclude it in our query. Next we loop through the categories of the current post, storing the category ID&#8217;s as a comma-separated string in <code>$allcats</code>. By then we have all the information we need to start query posts.</p>

<p>As parameters to <code>get_posts()</code>, we ask for three posts, in a descending order, in the categories stored in <code>$allcats</code> and finally, exclude current post. We use <code>setup_postdata()</code> to set up the post objects instead of <code>the_post()</code> as we loop through the posts. For each post we display post information by using the simple WordPress function such as <code>the_title()</code>.</p>

<p>After we&#8217;re done looping through the custom query we must remember to reset the global post ID by setting the global <code>$post</code> back into the object we stored in <code>$backup</code> and run the WordPress <code>wp_reset_query()</code> to reset the query. If we do this, WordPress is back on the original post object and can continue to display the comments, to the right single post.</p>

<p>The code works, but lacks styling. Let&#8217;s add some CSS for our related posts section in our <code>style.css</code>:</p>
<h4 style="font-size:13px;clear:both;">style.css:</h4>
<div class="wp_syntax">
<pre class="css">
<span class="g">/* Related posts */</span>
.<span class="l">related-template</span> { 
	<span class="l">background-color</span>: #ffffff;
	<span class="l">border</span>: 1px solid #dadcde; 
	<span class="l">padding</span>: 10px;
	<span class="l">margin</span>: 25px 20px 0 0;
}
.<span class="l">related-template h3</span> {
	<span class="l">font-family</span>: Arial, Verdana, sans-serif;
	<span class="l">font-size</span>: 18px;
	<span class="l">color</span>: #4f5051;
	<span class="l">padding-bottom</span>: 10px;
}
.<span class="l">related-template .related-posts-date</span> {
	<span class="l">font-size</span>: 11px;
	<span class="l">color</span>: #8c8e93;
}
</pre>
</div>
<div class="wp-caption alignleft" style="width: 160px; margin-top: 10px;"><a href="http://www.cappelendesign.no/wp-content/uploads/misc/wp-theme-tutorial/screenshot-relatedposts.jpg" target="_blank" class="shutterset_set_wptutlesson10"><img title="Related posts" src="http://www.cappelendesign.no/wp-content/uploads/misc/wp-theme-tutorial/thumbs/screenshot-relatedposts.jpg" alt="" /></a><p class="wp-caption-text">Related posts</p></div>
<p>We already have the styling for lists in place, the class <code>spaceinvader-list</code>, which we wrote in <a href="http://cappelendesign.no/?p=1497" title="WordPress Lesson 6: Styling our Theme with CSS">lesson 6</a>.</p>

<p>Open a single post in your WordPress implementation and scroll down to the end of the post before the comments section. You should see a box listing three (or less) posts, all of which should be in one or more of the same categories as the current post. The current single post will also never be one of the related posts.</p>

<h4>WordPress Codex Function references:</h4>
<ul>
<li><a href="http://codex.wordpress.org/Function_Reference/get_the_category" target="_blank" title="Wordpress Function Reference: get_the_category">get_the_category</a></li>
<li><a href="http://codex.wordpress.org/Function_Reference/get_posts" target="_blank" title="Wordpress Function Reference: get_posts">get_posts</a></li>
<li><a href="http://codex.wordpress.org/Function_Reference/the_title" target="_blank" title="Wordpress Function Reference: the_title">the_title</a></li>
<li><a href="http://codex.wordpress.org/Function_Reference/the_permalink" target="_blank" title="Wordpress Function Reference: the_permalink">the_permalink</a></li>
<li><a href="http://codex.wordpress.org/Function_Reference/the_time" target="_blank" title="Wordpress Function Reference: the_time">the_time</a></li>
<li><a href="http://codex.wordpress.org/Function_Reference/wp_reset_query" target="_blank" title="Wordpress Function Reference: wp_reset_query">wp_reset_query</a></li>
</ul>

<p>This was a category-wise way example of fetching related posts. There are a number of more advanced related posts plugins you could use instead, but now you&#8217;ve been introduced in one way to manage nested loops.</p>

<p>We will revisit the multiple loop issue in <a href="http://cappelendesign.no/?p=2711" title="WordPress Theme Lesson 12: Featured Posts, Dealing with Multiple Loops">lesson 12</a> when we write code for the featured posts on top of the front page. In that case it&#8217;s not nested loops but multiple loops on the same page, for which we need to handle queries and loops a little differently. But I recommend reading the <a href="http://cappelendesign.no/?p=1962" title="WordPress Theme Lesson 11: Including Javascripts the Right Way">next lesson</a> first, as I go through how to include javascript libraries and scripts which we&#8217;ll need for the featured posts section.</p>

<p><em>This concludes Lesson 10 of the <a href="http://cappelendesign.no/?p=1229" title="WordPress Theme Tutorial in 14 Lessons">WordPress Theme Tutorial</a>. Go to the next lesson, <a href="http://cappelendesign.no/?p=1962" title="WordPress Theme Lesson 11: Including Javascripts the Right Way">Including Javascripts the Right Way</a>, or back to the tutorial&#8217;s <a href="http://cappelendesign.no/?p=1229#toc" title="WordPress Theme Tutorial in 14 Lessons Table of Contents">Table of Contents</a>.</em></p>]]></content:encoded>
			<wfw:commentRss>http://cappelendesign.no/2011/10/wordpress-theme-lesson-10-related-posts-dealing-with-nested-loops/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>WordPress Theme Lesson 9: Adding Support for Custom Menu</title>
		<link>http://cappelendesign.no/2011/10/wordpress-theme-lesson-9-adding-support-for-custom-menu/</link>
		<comments>http://cappelendesign.no/2011/10/wordpress-theme-lesson-9-adding-support-for-custom-menu/#comments</comments>
		<pubDate>Sat, 22 Oct 2011 18:14:01 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Web Coding]]></category>
		<category><![CDATA[wordpress]]></category>
		<category><![CDATA[wordpress theme tutorial]]></category>

		<guid isPermaLink="false">http://www.acornartwork.com/blog/?p=1806</guid>
		<description><![CDATA[In version 3.0 WordPress launched a custom navigation menu system, allowing users to create custom menus in WordPress admin without the need of editing the theme code. If you&#8217;re not familiar with this yet, this is a great and informative Codex page you should read. The traditional way to organize a menu in WordPress was [...]]]></description>
			<content:encoded><![CDATA[<div class="wp-caption alignleft"><img title="Wordpress theme tutorial" src="http://www.cappelendesign.no/wp-content/uploads/misc/wp-theme-tutorial/Wordpress_tutorial.png" alt="" /></div>
<p>In version 3.0 WordPress launched a <a href="http://codex.wordpress.org/Navigation_Menus" target="_blank" title="WordPress Codex on Navigation Menus">custom navigation menu system</a>, allowing users to create custom menus in WordPress admin without the need of editing the theme code. If you&#8217;re not familiar with this yet, <a href="http://codex.wordpress.org/Appearance_Menus_SubPanel" title="WordPress Codex on the Menu Subpanel" target="_blank">this is a great and informative Codex page</a> you should read. The traditional way to organize a menu in WordPress was to create pages, and list all pages in the menu. As listing pages has certain obvious drawbacks, for instance you might not want to display <em>all</em> of your pages or you might want to use pages for something else contentwise rather than let them make up the site&#8217;s main menu, the new custom menu system provides the author greater flexibility and an user-friendly interface.</p>

<p>So far in the tutorial we&#8217;ve used the traditional way to organize our menu in the sidebar and footer, or maybe you chose to manually write out your menu items in the templates. The theme user/blogger/you don&#8217;t have a simple way to modify the site&#8217;s menu &#8211; the only way to change it is to edit the theme files, requiring knowledge in HTML and perhaps even PHP. That&#8217;s no good. So in this lesson we&#8217;ll replace our menus with support for the new custom menu system.</p>
<span id="more-1806"></span>

<p><strong>Note:</strong> If you use a WordPress version lower than 3.0, you can skip this lesson. It&#8217;s however highly recommended to regularily upgrade WordPress, to get new features as well as security updates.</p>

<p>Go to <code>WordPress admin &raquo; Appearance</code> and click on the subitem Menus. You will get a message stating that the current theme doesn&#8217;t support menus. But you are still allowed to create a custom menu and use a WordPress widget to place the menu. This is another solution, but less intuitive for the blog user. We want the user to simply create a menu, and in the same place choose where it should appear. Period.</p>

<p>The process for adding support for custom menus is two-parted. We create a PHP function in our <code>functions.php</code> which contains code to define a menu &#8220;container&#8221;, and by using action hooks we hook this function onto one of WordPress&#8217;s site initializing processes. Then we add a piece of code in the template where we want the menu container to be placed. After this we can use the Menu user interface and choose the desired container for our menu.</p>

<a name="navmenu"></a><h3>Adding Support for Custom Menu</h3>
<p>We have two WordPress functions we can use for adding support for custom menus, <code>register_nav_menu()</code> which registers one menu, and <code>register_nav_menus()</code> to register more than one. In our case we only need one (it is the same menu which appears in two different places). Let&#8217;s add this code in our <code>functions.php</code>.</p>
<h4 style="font-size:13px;clear:both;">functions.php:</h4>
<div class="wp_syntax">
<pre class="php">
<span class="g">/* Register main menu */</span>
function <span class="l">wpthemetutorial_register_menu</span>() {
	<span class="l">register_nav_menu</span>(<span class="r">'main-menu'</span>, __(<span class="r">'Main menu'</span>));
}
<span class="l">add_action</span>(<span class="r">'init'</span>, <span class="r">'wpthemetutorial_register_menu'</span>);
</pre>
</div>
<p>The first parameter is the menu slug (a shorthand name without spaces and no uppercase letters), and the second is our menu&#8217;s full name. Always give a name which best explains the user where this menu is supposed to appear, such as &#8220;Top navigation bar&#8221;, or &#8220;Main menu&#8221;. We then hook our function onto <code>init</code>, one of WordPress&#8217; core initializing states.</p>

<div class="wp-caption alignleft" style="width: 110px; margin-top: 10px;"><a href="http://www.cappelendesign.no/wp-content/uploads/misc/wp-theme-tutorial/custom-menu-dropdown.jpg" target="_blank" class="shutterset_set_wptutlesson9"><img title="Choosing menu location" src="http://www.cappelendesign.no/wp-content/uploads/misc/wp-theme-tutorial/thumbs/custom-menu-dropdown.jpg" alt="" /></a><p class="wp-caption-text">Choosing menu location</p></div>

<p>If you return to <code>Appearance &raquo; Menus</code> in WordPress admin, you should see that the previous warning is gone. Now, by using the WordPress&#8217;s custom menu editor, create a new menu by adding menu items and organizing them. In the box on the left-hand side titled <code>Theme Locations</code>, you&#8217;ll see that our theme supports a single menu, and the drop down list contains all menu containers you have created. We&#8217;ve created one, named <code>Main menu</code>. Select it in the dropdown-list and save. As of now the menu you created is what will be used as our main menu. The next and last step is add a line in the templates where we want the menu to appear.</p>

<h4>WordPress Codex Function references:</h4>
<ul>
<li><a href="http://codex.wordpress.org/Function_Reference/register_nav_menu" target="_blank" title="Wordpress Function Reference: register_nav_menu">register_nav_menu</a></li>
<li><a href="http://codex.wordpress.org/Function_Reference/register_nav_menus" target="_blank" title="Wordpress Function Reference: register_nav_menus">register_nav_menus</a></li>
<li><a href="http://codex.wordpress.org/Function_Reference/add_action" target="_blank" title="Wordpress Function Reference: add_action">add_action</a></li>
</ul>

<h3>Placing the Custom Menu in our Templates</h3>
<p>We use the WordPress function <code>wp_nav_menu()</code> to tell WordPress where the custom menu is to appear. As arguments we provide the menu slug defined in <code>functions.php</code> above, in our case <code>main-menu</code>. In our theme we want this menu to be displayed both in the sidebar and the footer so we need to edit both templates. Let&#8217;s start with the sidebar.</p>

<p>Open up <code>sidebar.php</code> and find the menu area where we list our pages (or all items if you chose to do write your own). Delete the whole <code>&lt;ul&gt;</code> list part, including the <code>wp_list_pages()</code> and the closing <code>&lt;/ul&gt;</code>. In its place add this line of code, which should be right after the <code>&lt;h2</code> with the widget title:</p>
<h4 style="font-size:13px;clear:both;">sidebar.php:</h4>
<div class="wp_syntax">
<pre class="php">
&lt;?php <span class="l">wp_nav_menu</span>(<span class="l">array</span>(<span class="r">'theme_location'</span> => <span class="r">'main-menu'</span>, <span class="r">'menu_class'</span> => <span class="r">'sidebar-menu'</span>)); ?>
</pre>
</div>
<p>The first argument in the <code>wp_nav_menu()</code> function call, the <code>theme_location</code>, is the important link pointing to the slug (the shorthand name, not the full name) you provided for your menu in the <code>functions.php</code>. I&#8217;ve also added an argument to <code>menu_class</code> so WordPress will give the menu&#8217;s <code>&lt;ul&gt;</code>-element the class <code>sidebar-menu</code>. In <a href="http://cappelendesign.no/?p=1497" title="Read Lesson 6: Styling our Theme with CSS">lesson 6</a> we added styling for a menu with this exact class name, so we don&#8217;t need to add any additional styling. It&#8217;ll look just like before, but with your custom menu items!</p>

<p>Keep in mind that if you call <code>wp_nav_menu()</code> without the <code>theme_location</code> argument, WordPress will display the first custom menu created, regardless. If there are no menus defined it will fall back to a list of pages.</p>

<p>Let&#8217;s repeat the procedure in our right widget area in the footer. The code is identical except for the <code>footer-menu</code> as class name. First, delete the old piece of menu code, including its wrapping <code>&lt;ul&gt;</code>. Then add this line of code in its place:</p>
<h4 style="font-size:13px;clear:both;">footer.php:</h4>
<div class="wp_syntax">
<pre class="php">
&lt;?php <span class="l">wp_nav_menu</span>(<span class="l">array</span>(<span class="r">'theme_location'</span> => <span class="r">'main-menu'</span>, <span class="r">'menu_class'</span> => <span class="r">'footer-menu'</span>)); ?>
</pre>
</div>
<p>If you refresh your site now, you should see the exact same menu items in your sidebar and your footer. The styling should be in order as well.</p>

<p>The best advantage is that, now this is done, it is very easy to add, edit, remove and organize your menu items using WordPress custom menu system. You don&#8217;t need to edit the theme files, and make sure that the sidebar and footer both have the exact same menu items.</p>
<h4>WordPress Codex Function references:</h4>
<ul>
<li><a href="http://codex.wordpress.org/Function_Reference/wp_nav_menu" target="_blank" title="Wordpress Function Reference: wp_nav_menu">wp_nav_menu</a></li>
</ul>

<p>Remember I mentioned earlier that it&#8217;s possible to use a WordPress widget to display the menu? Suppose you want to replace the default widgets in the footer and place the menu to the left, you can then use a widget to display your custom menu. The widget is called <code>Custom Menu</code> and allows you to choose which menu you want to display. Using a widget is a possibility but has the disadvantage that you can&#8217;t give the menu a class, thus making it hard to style with CSS.</p>

<p><em>This concludes Lesson 9 of the <a href="http://cappelendesign.no/?p=1229" title="WordPress Theme Tutorial in 14 Lessons">WordPress Theme Tutorial</a>. Go to the next lesson, <a href="http://cappelendesign.no/?p=1825" title="Lesson 10: Related Posts, Dealing with Nested Loops">Related Posts, Dealing with Nested Loops</a>, or back to the tutorial&#8217;s <a href="http://cappelendesign.no/?p=1229#toc" title="WordPress Theme Tutorial in 14 Lessons Table of Contents">Table of Contents</a>.</em></p>]]></content:encoded>
			<wfw:commentRss>http://cappelendesign.no/2011/10/wordpress-theme-lesson-9-adding-support-for-custom-menu/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>WordPress Theme Lesson 8: Post Thumbnails</title>
		<link>http://cappelendesign.no/2011/10/wordpress-theme-lesson-8-post-thumbnails/</link>
		<comments>http://cappelendesign.no/2011/10/wordpress-theme-lesson-8-post-thumbnails/#comments</comments>
		<pubDate>Wed, 12 Oct 2011 20:40:40 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Web Coding]]></category>
		<category><![CDATA[wordpress]]></category>
		<category><![CDATA[wordpress theme tutorial]]></category>

		<guid isPermaLink="false">http://www.acornartwork.com/blog/?p=1654</guid>
		<description><![CDATA[In this lesson we&#8217;ll go through post thumbnails, a feature introduced in WordPress 2.9. A post thumbnail is an image chosen by the author as the representative image for a post. Having an image to each post makes the site more interesting to look at. Keep in mind that the post thumbnail is not a [...]]]></description>
			<content:encoded><![CDATA[<div class="wp-caption alignleft"><img title="Wordpress theme tutorial" src="http://www.cappelendesign.no/wp-content/uploads/misc/wp-theme-tutorial/Wordpress_tutorial.png" alt="" /></div>
<p>In this lesson we&#8217;ll go through post thumbnails, a feature introduced in <a href="http://codex.wordpress.org/Version_2.9" title="WordPress Codex on WordPress 2.9">WordPress 2.9</a>. A post thumbnail is an image chosen by the author as the representative image for a post. Having an image to each post makes the site more interesting to look at. Keep in mind that the post thumbnail is not a part of the post&#8217;s body content, so it will not appear unless you choose it to.</p>

<p>In our design we want a wide and quite narrow thumbnail above each post on our front page. We also want the same post thumbnail but smaller and square for our featured posts section (the slider we&#8217;ll create at the very top of the front page). In this lesson we&#8217;ll first add and define these new image sizes and then tell WordPress where to display them. Along the way we will also add some code to exclude images that appear in posts, as not to make the front page look like a mess.</p>
<span id="more-1654"></span>

<p>First, a couple of things to note: The feature is generally referred to as post thumbnails, but when the author is to choose an image when editing a post, it will be referenced as &#8220;featured image&#8221;. And keep in mind that your web server needs the <a href="http://www.libgd.org/Main_Page" target="_blank" title="About the GD library">GD library</a> installed in order to have WordPress make copies of images and resize them. You most probably already have this installed. But if you upload images and WordPress can&#8217;t seem to make any copies with different sizes, the GD library might not be installed.</p>

<p>First, in our <code>functions.php</code> we need a line adding theme support for post thumbnails, which enables the featured image box in Edit Post in WordPress admin. Then we add code for defining new image sizes, providing unique names and dimensions. Finally we edit our templates, telling WordPress where and which image size we want to display. At the very end of the lesson I&#8217;ve added an optional code which filters <code>the_content()</code>, removing all <code>&lt;img&gt;</code> tags, on the front page only. Having the wide post thumbnail and another image just below it (suppose the author has added an image at the beginning of the post) will not look very good.</p>

<h3>Adding Support for and Inserting Post Thumbnails</h3>
<p>First, we add the theme support for post thumbnails in our <code>functions.php</code>, and by doing so we enable the featured image selector in Edit Post in WordPress admin. Place this anywhere in the functions-file:</p>
<h4 style="font-size:13px;clear:both;">functions.php:</h4>
<div class="wp_syntax">
<pre class="php">
<span class="l">add_theme_support</span>(<span class="r">'post-thumbnails'</span>);
</pre>
</div>

<div class="wp-caption alignleft" style="width: 260px;margin-top: 10px;"><img title="The Featured Image option in Edit Post" src="http://www.cappelendesign.no/wp-content/uploads/misc/wp-theme-tutorial/featuredimage1.jpg" alt="" /><p class="wp-caption-text">The Featured Image option in Edit Post</p></div>
<p>Save, go to WordPress admin and edit a post. Look to your right, below the Post tags which resides below Categories and the Publish box. Clicking on the link will take you to the well-known interface for uploading images or choosing images from the library. But for adding post thumbnail you are <strong>not</strong> to click the &#8220;Insert into Post&#8221; button.</p>

<div class="wp-caption alignright" style="width: 260px;margin-top: 10px;"><img title="The Featured Image link in Add Media" src="http://www.cappelendesign.no/wp-content/uploads/misc/wp-theme-tutorial/featuredimage2.jpg" alt="" /><p class="wp-caption-text">The Featured Image link in Add Media</p></div>
<p>If you see to the right of the &#8220;Insert into Post&#8221; button, you will see a link &#8220;Use as featured image&#8221;. Whenever you choose a post thumbnail, this is what you click, not the &#8220;Insert into Post&#8221; button. The Image dialog box won&#8217;t close by itself when you click it (just click the X in the corner), but when you close it you will see a thumbnail of your chosen image inside the Featured image box, with an option to remove it.</p>

<p>Do not start uploading images just yet. As for now WordPress will make three copies of your uploaded images, the default <code>Thumbnail</code>, <code>Medium</code> and <code>Large</code>. We want to add a couple of more sizes, telling WordPress to create more copies with our desired heights and widths.</p>

<h3>Adding New Custom Image Sizes</h3>
<p>Adding a new custom image size is pretty simple, we call a WordPress function in our <code>functions.php</code> for each new image size, providing an unique name, the width and height as arguments. We will add two sizes, one for the front page and one for the featured posts section. Add these lines anywhere in our functions-file:</p>
<h4 style="font-size:13px;clear:both;">functions.php:</h4>
<div class="wp_syntax">
<pre class="php">
<span class="l">add_image_size</span>(<span class="r">'frontpage-thumbnail'</span>, 523, 105, TRUE);
<span class="l">add_image_size</span>(<span class="r">'featuredposts-thumbnail'</span>, 140, 100, TRUE);
</pre>
</div>
<p>The first parameter to <code>add_image_size()</code> is a chosen shorthand name of our image size, without spaces and  no uppercase letters. Whenever we insert a post thumbnail in a template, we will refer to this name. The next two arguments define the width and height, respectively. The last parameter decides whether or not WordPress is allowed to crop the image to force the images to fit into the given dimensions.</p>

<p>With these two lines in your <code>functions.php</code> you can now upload images, and see that five copies are made, the three default <code>Thumbnail</code>, <code>Medium</code> and <code>Large</code>, and our two new sizes. If you already have a Library filled with images, there won&#8217;t exist copies in our two new sizes. For this you can use the <a href="http://wordpress.org/extend/plugins/regenerate-thumbnails/" target="_blank" title="Wordpress plugin: Regenerate thumbnails">Regenerate thumbnails</a> plugin to create new copies of already uploaded images.</p>

<h3>Displaying Post Thumbnails in Templates</h3>
<p>Now that we&#8217;ve added the support, defined custom image sizes, and uploaded a least a couple of images for testing purposes, it&#8217;s time to tell WordPress to display them. For this we edit the template <code>index.php</code>, inserting the <code>frontpage-thumbnail</code> in each post. As we haven&#8217;t written our featured posts section yet, I will only show how you insert the post thumbnails at the front page.</p>

<p>Insert this line <em>after</em> the <code>&lt;h3&gt;</code> post title and <em>before</em> the <code>&lt;div&gt;</code> with the class <code>postcontent</code>:</p>
<h4 style="font-size:13px;clear:both;">index.php:</h4>
<div class="wp_syntax">
<pre class="php">
&lt;?php <span class="l">if</span> (<span class="l">has_post_thumbnail</span>()) : ?&gt;&lt;div <span class="b">class</span>=<span class="r">"postthumbnail"</span>&gt;&lt;?php <span class="l">the_post_thumbnail</span>(<span class="r">'frontpage-thumbnail'</span>); ?&gt;</span>&lt;/div&gt;&lt;?php <span class="l">endif</span>; ?&gt;
</pre>
</div>
<p>What this line does is to first check whether or not the post has been assigned a post thumbnail. If it does, we insert a wrapping <code>&lt;div&gt;</code> and call the WordPress function <code>the_post_thumbnail()</code> to insert the image. As parameter to <code>the_post_thumbnail()</code> we provide the name of the size we want, in our case <code>frontpage-thumbnail</code>. And that&#8217;s it for displaying the post thumbnail!</p>
<p>Let&#8217;s add a small piece of CSS into our <code>style.css</code> to improve the look of the post thumbnail images:</p>
<h4 style="font-size:13px;clear:both;">style.css:</h4>
<div class="wp_syntax">
<pre class="css">
.<span class="l">post .postthumbnail</span> { 
	<span class="l">border</span>: 1px solid #697f9a;
	<span class="l">width</span>: 515px;
	<span class="l">margin</span>: 6px 0;
}
</pre>
</div>
<p>Save, refresh your front page and see that all posts that has a post thumbnail assigned to it, get a wide image after the title. The post thumbnail is only visible on your front page, not on single views or the other templates, such as the search results or archives. It&#8217;s up to you if you want to add post thumbnails to any of the other templates. Just copy the line we inserted into <code>index.php</code> and paste it in the desired template.</p>

<div class="wp-caption alignleft" style="width: 110px;margin-top: 10px;"><a href="http://www.cappelendesign.no/wp-content/uploads/misc/wp-theme-tutorial/screenshot3.jpg"><img title="Our frontpage with post thumbnails" src="http://www.cappelendesign.no/wp-content/uploads/misc/wp-theme-tutorial/thumbs/screenshot3.jpg" alt="" /></a><p class="wp-caption-text">Our frontpage with post thumbnails</p></div>

<p>If your site already has a bunch of posts and you don&#8217;t want to edit each post and set a post thumbnail, there&#8217;s a plugin that can help you with that. The <a href="http://wordpress.org/extend/plugins/auto-post-thumbnail/" target="_blank" title="Wordpress plugin: Auto Post Thumbnail">Auto post thumbnail</a> plugin will generate a post thumbnail from the first image in your post. Keep in mind that you probably need to run the <a href="http://wordpress.org/extend/plugins/regenerate-thumbnails/" target="_blank" title="Wordpress plugin: Regenerate thumbnails">Regenerate thumbnails</a> plugin in order to make copies of all images in the sizes we&#8217;ve defined here.</p>

<p>It&#8217;s starting to look good, doesn&#8217;t it? But if you&#8217;re like me, who like to use images in posts, and most often quite early in the post, it may look a little messy with the wide post thumbnail, and a image right below it. In our design it looks more tidy with text only and without any additional cluttering images. That&#8217;s why I&#8217;ve added a piece of code to exclude all images from the post content on the front page. It&#8217;s fully optional to add this piece of code, but it could prove useful to read it as this is the first <a href="http://codex.wordpress.org/Plugin_API/Filter_Reference" target="_blank" title="Wordpress Codex Filter Reference">WordPress filter</a> I&#8217;m explaining in this tutorial. If you recall <a href="http://cappelendesign.no/?p=1251#hooks">Lesson 1</a> this is the other kind of WordPress hooks.</p>

<h4>WordPress Codex Function references:</h4>
<ul>
<li><a href="http://codex.wordpress.org/Function_Reference/add_theme_support" target="_blank" title="Wordpress Function Reference: add_theme_support">add_theme_support</a></li>
<li><a href="http://codex.wordpress.org/Function_Reference/add_image_size" target="_blank" title="Wordpress Function Reference: add_image_size">add_image_size</a></li>
<li><a href="http://codex.wordpress.org/Function_Reference/has_post_thumbnail" target="_blank" title="Wordpress Function Reference: has_post_thumbnail">has_post_thumbnail</a></li>
<li><a href="http://codex.wordpress.org/Function_Reference/the_post_thumbnail" target="_blank" title="Wordpress Function Reference: the_post_thumbnail">the_post_thumbnail</a></li>
</ul>

<h3 style="clear:both;">Our First Filter: Exclude All Images in Posts at the Front Page</h3>
<p>This optional piece of code hooks onto a WordPress filter, as opposed to actions. The method is similar as to what we&#8217;ve done with actions earlier; we write a function that modifies a piece of information returned by WordPress, hooks it onto hooks such as <code>the_content()</code>, <code>the_title()</code> or almost any function which returns or stores a piece of information in WordPress, and WordPress will automatically run our function when the situation appears. See a list of all <a href="http://codex.wordpress.org/Plugin_API/Filter_Reference" target="_blank" title="Wordpress Codex Filter Reference">available filters here</a>.</p>

<p>We are to write a filter on <code>the_content()</code>, the function we&#8217;ve used in our templates and <code>index.php</code> to display the post content. Basically what we do is using a regular expression to find <code>&lt;img&gt;</code> tags and cut them out, but do so only on the front page. Otherwise all images are cut away from all templates, even single post views. It&#8217;s important to remember that we&#8217;ve used <code>the_content()</code> on basically every template, and filtering it will affect them all.</p>

<p>Writing a filter is similar to action hooks, we use <code>add_filter()</code> with the filter hook we want to hook this function onto as the first argument. But with filters we need to place arguments to our PHP functions, which is the piece(s) of information we are to modify. More importantly we must remember to return all values when we&#8217;re done. If we forget to return it, the filter we&#8217;ve hooked onto will return empty. In our case all calls to <code>the_content()</code> will return nothing, and that&#8217;s pretty disastrous!</p>
<h4 style="font-size:13px;clear:both;">functions.php:</h4>
<div class="wp_syntax">
<pre class="php">
<span class="g">/* Exclude images in the_content on home and frontpage */</span>
function <span class="l">wpthemetutorial_image_content_filter</span>(<span class="b">$content</span>) {
	<span class="l">if</span> (<span class="l">is_home</span>() || <span class="l">is_front_page</span>()) {
		<span class="b">$content</span> = <span class="l">preg_replace</span>(<span class="r">"&#47;&lt;img&#91;&#94;&gt;&#93;&#43;&#92;&gt;&#47;i"</span>, <span class="r">""</span>, <span class="b">$content</span>);
	}
	<span class="l">return</span> <span class="b">$content</span>;
}
<span class="l">add_filter</span>(<span class="r">'the_content'</span>, <span class="r">'wpthemetutorial_image_content_filter'</span>, 11);
</pre>
</div>
<p>The <code>the_content()</code> requires us to add one parameter to our PHP function, the entire post content which we store in the PHP value <code>$content</code>. First, and importantly, we check if we are on the front page by checking the <a href="http://codex.wordpress.org/Conditional_Tags" target="_blank" title="Wordpress Codex Conditional Tags">WordPress conditional tags</a> <code>is_home()</code> and <code>is_front_page()</code>. If that&#8217;s the case we run a PHP regular expression replacing any <code>&lt;img&gt;</code> in the content with an empty string. Then we return the content, regardless of what have been done in this function. By using <code>add_filter()</code> we tell WordPress to run our function whenever WordPress runs <code>the_content()</code>.</p>

<p>If you refresh your front page, it should still display post thumbnails, but no other images &#8211; not even smiley icons are displayed if you&#8217;ve enabled WordPress smileys.</p>

<h4>WordPress Codex Function references:</h4>
<ul>
<li><a href="http://codex.wordpress.org/Function_Reference/is_home" target="_blank" title="Wordpress Function Reference: is_home">is_home</a></li>
<li><a href="http://codex.wordpress.org/Function_Reference/is_front_page" target="_blank" title="Wordpress Function Reference: is_front_page">is_front_page</a></li>
<li><a href="http://codex.wordpress.org/Function_Reference/add_filter" target="_blank" title="Wordpress Function Reference: add_filter">add_filter</a></li>
</ul>

<p><em>This concludes Lesson 8 of the <a href="http://cappelendesign.no/?p=1229" title="WordPress Theme Tutorial in 14 Lessons">WordPress Theme Tutorial</a>. Go to the next lesson, <a href="http://cappelendesign.no/?p=1806" title="Lesson 9: Adding Support for Custom Menus">Adding Support for Custom Menus</a>, or back to the tutorial&#8217;s <a href="http://cappelendesign.no/#toc" title="Table of Contents of WordPress Theme Tutorial in 14 Lessons">Table of Contents</a>.</em></p>]]></content:encoded>
			<wfw:commentRss>http://cappelendesign.no/2011/10/wordpress-theme-lesson-8-post-thumbnails/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>WordPress Theme Lesson 7: Defining Multiple Widget Areas and Default Wrappings</title>
		<link>http://cappelendesign.no/2011/10/wordpress-theme-lesson-7-defining-multiple-widget-areas-and-customizing-default-wrappings/</link>
		<comments>http://cappelendesign.no/2011/10/wordpress-theme-lesson-7-defining-multiple-widget-areas-and-customizing-default-wrappings/#comments</comments>
		<pubDate>Wed, 12 Oct 2011 20:20:29 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Tutorials]]></category>
		<category><![CDATA[Web Coding]]></category>
		<category><![CDATA[wordpress]]></category>
		<category><![CDATA[wordpress theme tutorial]]></category>
		<category><![CDATA[wordpress widgets]]></category>

		<guid isPermaLink="false">http://www.acornartwork.com/blog/?p=1608</guid>
		<description><![CDATA[In this lesson we&#8217;ll dig further into widgets by adding multiple widget areas and further customizing them. We are to add three widget areas in the footer, and another widget area in our sidebar. By adding two widget areas in the sidebar, one above and one below the ads element, we give the user flexibility [...]]]></description>
			<content:encoded><![CDATA[<div class="wp-caption alignleft"><img title="Wordpress theme tutorial" src="http://www.cappelendesign.no/wp-content/uploads/misc/wp-theme-tutorial/Wordpress_tutorial.png" alt="" /></div>
<p>In this lesson we&#8217;ll dig further into widgets by adding multiple widget areas and further customizing them. We are to add three widget areas in the footer, and another widget area in our sidebar. By adding two widget areas in the sidebar, one above and one below the ads element, we give the user flexibility in organizing the sidebar. Especially if the ad element are to be hardcoded in the sidebar.</p>

<p>Defining multiple widget areas require us to write a little more code since we need to identify each area with a name and an ID. At the same time we will also define which wrappings should be used around and in all widgets that are placed in the areas. In the <a href="http://cappelendesign.no/?p=1497" title="Read the previous lesson: Styling our Theme With CSS">previous lesson</a> when we tried to style widgets in our widget area in the sidebar, we saw that WordPress automatically wraps all widgets inside <code>&lt;li&gt;</code> elements. By WordPress&#8217;s standards the sidebar should be an unordered list, and each widget a list item with a yet another list inside for its elements. I myself prefer wrapping widgets in <code>&lt;div&gt;</code>s, to skip the messy work of styling nested lists.</p>
<p>So we need to modify and add code to the templates where our widget areas should appear, in our case the <code>sidebar.php</code> and <code>footer.php</code>, associating each with the registering we do in our <code>functions.php</code>.</p>
<span id="more-1608"></span>

<p>We start by opening our <code>functions.php</code>. The file should contain a previously written line of code which defines our single widget area in the sidebar:</p>
<div class="wp_syntax">
<pre class="php">
<span class="l">if</span> (<span class="l">function_exists</span>(<span class="r">'register_sidebar'</span>)) <span class="l">register_sidebar</span>();
</pre>
</div>
<p>It&#8217;s nice and neat, but the function <code>register_sidebar()</code> can do much more. We can add arguments defining its name and HTML wrappings.</p>
<p><strong>Delete</strong> this line, and let&#8217;s add something better instead.</p>

<h3>Defining Multiple Widget Areas</h3>
<p>For defining multiple widget areas, we simply call <code>register_sidebar()</code> repeatedly for each widget area we want to add. But keep in mind that the names we give to each widget area only affects the names appearing in <code>WordPress admin &raquo; Appearance &raquo; Widgets</code>. There are no actual relation to the widget area definition and the <code>dynamic_sidebar()</code> calls we are to write in our templates (which defines <em>where</em> our widget areas are). So how do we know which definition goes where? Starting at 1, each <code>register_sidebar()</code> function call are given the ID of 1, incrementing 1 by each <code>register_sidebar()</code> call. In our theme we will get widget areas ID-ed 1 to 5. Then in our templates, we use these numbers as parameters to the <code>dynamic_sidebar()</code>. Still with me? It&#8217;s really not that complicated, but it may be easier to understand if you see the code in action.</p>

<p>Before I show you the code I want to also arrange all five <code>register_sidebar()</code> function calls inside a neat function. But as functions are not run automatically, we need WordPress to run it for us on a specific occasion. For this we hook our function onto the hook where WordPress initializes widgets. If you&#8217;re unsure what I mean by hooks, read <a href="http://cappelendesign.no/?p=1251#hooks" title="Read about hooks in Lesson 1">the section about hooks in Lesson 1</a>.</p>

<p>To sum up, we are doing the following: We call <code>register_sidebar()</code> five times, in each call we give the widget area a name and define the wrappings we want around the widget and around the widget title. All five calls are gathered inside a PHP function. We then tell WordPress that every time WordPress initializes widgets, this function is to be run.</p>
<p>If you have not already done so, delete the single line of <code>register_sidebar()</code>, leaving our functions-file empty (except for the PHP tag). Our full <code>functions.php</code> should look like:</p>
<h4 style="font-size:13px;clear:both;">functions.php:</h4>
<div class="wp_syntax" style="height:350px;overflow:auto;">
<pre class="php">
&lt;?php 
<span class="g">/* Register widget areas */</span>
function <span class="l">wpthemetutorial_widgets_init</span>() {
	
	<span class="g">// #1 - first area in sidebar.php</span>
	<span class="l">if</span> ( <span class="l">function_exists</span>(<span class="r">'register_sidebar'</span>) )
		<span class="l">register_sidebar</span>(<span class="l">array</span>(
			<span class="r">'name'</span> =&gt; <span class="r">'Sidebar - Above advertise area'</span>,
			<span class="r">'before_title'</span> =&gt; <span class="r">'&lt;h2 class="widgettitle"&gt;'</span>,
			<span class="r">'after_title'</span> =&gt; <span class="r">'&lt;/h2&gt;'</span>,
			<span class="r">'before_widget'</span> =&gt; <span class="r">'&lt;div class="sidebar-box"&gt;'</span>,
			<span class="r">'after_widget'</span> =&gt; <span class="r">'&lt;/div&gt;'</span>,
	));
	
	<span class="g">// #2 - second area in sidebar.php</span>
	<span class="l">if</span> ( <span class="l">function_exists</span>(<span class="r">'register_sidebar'</span>) )
		<span class="l">register_sidebar</span>(<span class="l">array</span>(
			<span class="r">'name'</span> =&gt; <span class="r">'Sidebar - Below advertise area'</span>,
			<span class="r">'before_title'</span> =&gt; <span class="r">'&lt;h2 class="widgettitle"&gt;'</span>,
			<span class="r">'after_title'</span> =&gt; <span class="r">'&lt;/h2&gt;'</span>,
			<span class="r">'before_widget'</span> =&gt; <span class="r">'&lt;div class="sidebar-box"&gt;'</span>,
			<span class="r">'after_widget'</span> =&gt; <span class="r">'&lt;/div&gt;'</span>,
	));
	
	<span class="g">// #3 - left area in footer</span>
	<span class="l">if</span> ( <span class="l">function_exists</span>(<span class="r">'register_sidebar'</span>) )
		<span class="l">register_sidebar</span>(<span class="l">array</span>(
			<span class="r">'name'</span> =&gt; <span class="r">'Footer - Left column'</span>,
			<span class="r">'before_title'</span> =&gt; <span class="r">'&lt;h2 class="widgettitle"&gt;'</span>,
			<span class="r">'after_title'</span> =&gt; <span class="r">'&lt;/h2&gt;'</span>,
			<span class="r">'before_widget'</span> =&gt; <span class="r">'&lt;div class="footer-box"&gt;'</span>,
			<span class="r">'after_widget'</span> =&gt; <span class="r">'&lt;/div&gt;'</span>,
	));
	
	<span class="g">// #4 - middle area in footer</span>
	<span class="l">if</span> ( <span class="l">function_exists</span>(<span class="r">'register_sidebar'</span>) )
		<span class="l">register_sidebar</span>(<span class="l">array</span>(
			<span class="r">'name'</span> =&gt; <span class="r">'Footer - Middle column'</span>,
			<span class="r">'before_title'</span> =&gt; <span class="r">'&lt;h2 class="widgettitle"&gt;'</span>,
			<span class="r">'after_title'</span> =&gt; <span class="r">'&lt;/h2&gt;'</span>,
			<span class="r">'before_widget'</span> =&gt; <span class="r">'&lt;div class="footer-box"&gt;'</span>,
			<span class="r">'after_widget'</span> =&gt; <span class="r">'&lt;/div&gt;'</span>,
	));
	
	<span class="g">// #5 - right area in footer</span>
	<span class="l">if</span> ( <span class="l">function_exists</span>(<span class="r">'register_sidebar'</span>) )
		<span class="l">register_sidebar</span>(<span class="l">array</span>(
			<span class="r">'name'</span> =&gt; <span class="r">'Footer - Right column'</span>,
			<span class="r">'before_title'</span> =&gt; <span class="r">'&lt;h2 class="widgettitle"&gt;'</span>,
			<span class="r">'after_title'</span> =&gt; <span class="r">'&lt;/h2&gt;'</span>,
			<span class="r">'before_widget'</span> =&gt; <span class="r">'&lt;div class="footer-box"&gt;'</span>,
			<span class="r">'after_widget'</span> =&gt; <span class="r">'&lt;/div&gt;'</span>,
	));
}
<span class="l">add_action</span>(<span class="r">'widgets_init'</span>, <span class="r">'wpthemetutorial_widgets_init'</span>);
</pre>
</div>
<p>Here you see the five <code>register_sidebar()</code> calls gathered in a function named <code>wpthemetutorial_widgets_init()</code>.</p>
<p><strong>Tip:</strong> You are free to give your functions (almost) any name you want, but you must make sure that all your functions have unique names &#8211; not only in your theme, but all over WordPress. If WordPress or any active plugin have the same function name as one in your theme, your theme will not work. This is why it&#8217;s a good idea to always start all your functions with a short version of your theme name, in this case <code>wpthemetutorial</code>.</p>

<p>As for the parameter array in each <code>register_sidebar()</code>, we use <code>name</code> to define the name to be displayed on each area when we are at Widgets in WordPress admin. The <code>before_title</code> and <code>after_title</code> parameters define the wrappings around the widget title, and <code>before_widget</code> and <code>after_widget</code> defines the widget wrappings. When we use any of these four parameters, we overwrite WordPress default values. We&#8217;ll now get <code>&lt;div&gt;</code>s instead of WordPress&#8217; default <code>&lt;li&gt;</code>s. You may have noticed that I&#8217;ve chosen to use a different class name in the footer widget areas, <code>footer-box</code> instead of <code>sidebar-box</code>, in order to specifically target footer widgets with CSS.</p>

<p>The very last line, the function call <code>add_action()</code>, is the &#8220;hooking onto actions&#8221;-function. It tells WordPress to hook onto the hook given as the first parameter, in our case <code>widgets_init</code>, and run the function given as second parameter, which is our <code>wpthemetutorial_widgets_init</code>.</p>

<div class="wp-caption alignleft" style="width: 120px; margin-top: 10px;"><a href="http://www.cappelendesign.no/wp-content/uploads/misc/wp-theme-tutorial/widgets1.jpg" target="_blank" class="shutterset_set_wptutlesson7"><img title="Our widget areas in admin" src="http://www.cappelendesign.no/wp-content/uploads/misc/wp-theme-tutorial/thumbs/widgets1.jpg" alt="" /></a><p class="wp-caption-text">Our widget areas in admin</p></div>

<p>If you go to <code>WordPress admin &raquo; Appearance &raquo; Widgets</code> you will see that there are five widget areas available we can insert and organize widgets into. Their names should correspond to the parameter <code>name</code> we defined in each <code>register_sidebar()</code>.</p>
<p>But we haven&#8217;t yet defined <em>where</em> each widget area should appear. Our next step is to edit <code>sidebar.php</code> and <code>footer.php</code> and call <code>dynamic_sidebar()</code> with the correct ID number as parameter. Let&#8217;s begin with the sidebar, where we already have one <code>dynamic_sidebar</code> function call.</p>

<h4 style="font-size:13px;">WordPress Codex Function references:</h4>
<ul>
<li><a href="http://codex.wordpress.org/Function_Reference/register_sidebar" target="_blank" title="Wordpress Function Reference: register_sidebar">register_sidebar</a></li>
<li><a href="http://codex.wordpress.org/Function_Reference/add_action" target="_blank" title="Wordpress Function Reference: add_action">add_action</a></li>
</ul>

<h3>Placing Widget Areas in our Templates</h3>
<p>If you take a look at the order of the <code>register_sidebar()</code> in our <code>functions.php</code> above, the first one is the top sidebar widget area, which means ID 1. The last one, the right footer widget area, has the ID of 5. We will use these IDs to identify which widget area is which in our templates.</p>
<h4>The Sidebar</h4>
<p>Open <code>sidebar.php</code> and find the <code>dynamic_sidebar()</code> function call. To make the widget area work coherently with our new definitions, all we need to do is just to add &#8220;1&#8243; as an argument to the function call. This is our <code>dynamic_sidebar()</code> after we&#8217;ve added the argument:</p>
<div class="wp_syntax">
<pre class="php">
&lt;?php <span class="l">if</span> (<span class="l">function_exists</span>(<span class="r">'dynamic_sidebar'</span>) &#038;&#038; <span class="l">dynamic_sidebar</span>(1)) : else : ?&gt;
</pre>
</div>
<p>Save, place a widget inside this widget area in WordPress admin and see that it appears in your sidebar, just like it did before, but it should now come in our chosen wrappings and should by styled correctly. Awesome! Let&#8217;s define the other widget area in our sidebar.</p>

<p>Basically copy everything inside and including the lines with <code>dynamic_sidebar()</code> and <code>endif</code> and paste it right below. Change the parameter in this second piece into &#8220;2&#8243; which corresponds to our second <code>register_sidebar()</code> call. It&#8217;s also preferable to change the content inside the <code>else</code> portion, as to not display the same widgets two times if there are no widgets placed in Wordpres admin. I&#8217;ve just left the categories widget in the first and the bookmark widget in the second widget area.</p>

<p>After the hard-coded search and share-element and menu, and before we close the sidebar <code>&lt;div&gt;</code>, this is what we have:</p>
<h4 style="font-size:13px;clear:both;">sidebar.php:</h4>
<div class="wp_syntax">
<pre class="php">
...
<span class="g">&lt;!-- First widget area (before ads) --&gt;</span>
<span class="y">&lt;?php <span class="l">if</span> (<span class="l">function_exists</span>(<span class="r">'dynamic_sidebar'</span>) &#038;&#038; <span class="l">dynamic_sidebar</span>(1)) : <span class="l">else</span> : ?&gt;</span>
&lt;div <span class="b">class</span>=<span class="r">"sidebar-box"</span>&gt;
	&lt;h2 <span class="b">class</span>=<span class="r">"widgettitle"</span>&gt;Categories&lt;/h2&gt;
	&lt;ul&gt;
		<span style="background:#ffffd2;">&lt;?php <span class="l">wp_list_categories</span>(<span class="r">'sort_column=name&#038;optioncount=0&#038;hierarchical=0&#038;title_li='</span>); ?&gt;</span>
	&lt;/ul&gt;
&lt;/div&gt;
<span class="y">&lt;?php <span class="l">endif</span>; ?&gt;</span>

&lt;div <span class="b">id</span>=<span class="r">"sidebar-ads"</span>&gt;&lt;/div&gt;
				
<span class="g">&lt;!-- Second widget area (below ads) --&gt;</span>
<span class="y">&lt;?php <span class="l">if</span> (<span class="l">function_exists</span>(<span class="r">'dynamic_sidebar'</span>) &#038;&#038; <span class="l">dynamic_sidebar</span>(2)) : <span class="l">else</span> : ?&gt;</span>
&lt;div <span class="b">class</span>=<span class="r">"sidebar-box"</span>&gt;
	&lt;h2 <span class="b">class</span>=<span class="r">"widgettitle"</span>&gt;Blogroll&lt;/h2&gt;
	&lt;ul&gt;
		<span class="y">&lt;?php <span class="l">wp_list_bookmarks</span>(<span class="r">'title_li=&#038;categorize=0&#038;category=2&#038;before=&lt;li&gt;&#038;after=&lt;/li&gt;&#038;show_images=0&#038;show_description=0&#038;orderby=name'</span>); ?&gt;</span>
	&lt;/ul&gt;
&lt;/div&gt;
<span class="y">&lt;?php <span class="l">endif</span>; ?&gt;</span>
...
</pre>
</div>
<p>I&#8217;ve placed an empty <code>&lt;div&gt;</code> from which ads can be placed into. If you&#8217;re planning to use ads as a widget this isn&#8217;t necessary.</p>

<h4>The Footer</h4>
<p>Now we simply repeat what we&#8217;ve done three times in our <code>footer.php</code>, using the number 3 through 5.</p>
<p>From our initial theme design I wanted the left widget to be a text and photo introducing the blog or author, the middle a list of most popular posts, and the right the main menu. We write these inside the <code>else</code> clause so it is displayed if the user hasn&#8217;t placed any widgets. The reason we don&#8217;t hard-code the footer is because we want to give the blogger the possibility to change the content and reorganize the footer from the WordPress admin interface instead of editing the theme files. But should the user choose not to change anything, the default should work just fine.</p>
<p>Our full <code>footer.php</code> should look like:</p>
<h4 style="font-size:13px;clear:both;">footer.php:</h4>
<div class="wp_syntax" style="height:350px;overflow:auto;">
<pre class="php">
				&lt;div <span class="b">style</span>=<span class="r">"clear:both;"</span>&gt;&lt;/div&gt;
			&lt;/div&gt; <span class="g">&lt;!-- end content-wrapper--&gt;</span>
		&lt;/div&gt; <span class="g">&lt;!-- end top-container--&gt;</span>
		
		&lt;div <span class="b">id</span>=<span class="r">"bottom-container"</span>&gt;
			&lt;div <span class="b">id</span>=<span class="r">"footer"</span>&gt;
			
				<span class="g">&lt;!-- Left column widget area --&gt;</span>
	            		<span class="y">&lt;?php <span class="l">if</span> (<span class="l">function_exists</span>(<span class="r">'dynamic_sidebar'</span>) &#038;&#038; <span class="l">dynamic_sidebar</span>(3)) : <span class="l">else</span> : ?&gt;</span>
				&lt;div <span class="b">class</span>=<span class="r">"footer-box"</span>&gt;
					&lt;h2 <span class="b">class</span>=<span class="r">"widgettitle"</span>&gt;Who am I?&lt;/h2&gt;
					&lt;span <span class="b">class</span>=<span class="r">"footer-gravatar"</span>&gt;<span class="y">&lt;?php <span class="l">echo get_avatar</span>(1, 50); ?&gt;</span>&lt;/span&gt;
					&lt;p&gt;Hi, I'm Name Lastname, the woman behind this site. Dolor sit amet. Lorem Ipsum dolor sit amet consectetur adipiscing elit. Pellentesque habitant morbi tristique. Phasellus vehicula magna fringilla.&lt;/p&gt;
				&lt;/div&gt;
				<span class="y">&lt;?php <span class="l">endif</span>; ?&gt;</span>
				
				<span class="g">&lt;!-- Middle column widget area --&gt;</span>
	            		<span class="y">&lt;?php <span class="l">if</span> (<span class="l">function_exists</span>(<span class="r">'dynamic_sidebar'</span>) &#038;&#038; <span class="l">dynamic_sidebar</span>(4)) : <span class="l">else</span> : ?&gt;</span>
				&lt;div <span class="b">class</span>=<span class="r">"footer-box"</span>&gt;
					&lt;h2 <span class="b">class</span>=<span class="r">"widgettitle"</span>&gt;Recent posts&lt;/h2&gt;
					&lt;ul&gt;
						<span class="y">&lt;?php</span>
						<span class="y"><span class="b">$postslist</span> = <span class="l">get_posts</span>(<span class="r">'posts_per_page=5'</span>);</span>
						<span class="y"><span class="l">foreach</span> (<span class="b">$postslist</span> as <span class="b">$post</span>) :  <span class="l">setup_postdata</span>(<span class="b">$post</span>); ?&gt;</span>
						&lt;li&gt;&lt;a <span class="b">href</span>=<span class="r">"</span><span class="y">&lt;?php <span class="l">the_permalink</span>(); ?&gt;</span><span class="r">"</span> <span class="b">title</span>=<span class="r">"</span><span class="y">&lt;?php <span class="l">the_title</span>(); ?&gt;</span><span class="r">"</span>&gt;<span class="y">&lt;?php <span class="l">the_title</span>(); ?&gt;</span>&lt;/a&gt;&lt;/li&gt;
						<span class="y">&lt;?php <span class="l">endforeach</span>; ?&gt;</span>
					&lt;/ul&gt;
				&lt;/div&gt;
				<span class="y">&lt;?php <span class="l">endif</span>; ?&gt;</span>
				
				<span class="g">&lt;!-- Right column widget area --&gt;</span>
	            		<span class="y">&lt;?php <span class="l">if</span> (<span class="l">function_exists</span>(<span class="r">'dynamic_sidebar'</span>) &#038;&#038; <span class="l">dynamic_sidebar</span>(5)) : <span class="l">else</span> : ?&gt;</span>
				&lt;div <span class="b">class</span>=<span class="r">"footer-box"</span>&gt;
					&lt;h2 <span class="b">class</span>=<span class="r">"widgettitle"</span>&gt;Main menu&lt;/h2&gt;
					&lt;ul <span class="b">class</span>=<span class="r">"sidebar-menu"</span>&gt;
						<span class="y">&lt;?php <span class="l">wp_list_pages</span>(<span class="r">'title_li=&#038;depth=-1'</span>); ?&gt;</span>
					&lt;/ul&gt;
					&lt;div <span class="b">style</span>=<span class="r">"clear:both;</span>"&gt;&lt;/div&gt;
				&lt;/div&gt;
				<span class="y">&lt;?php <span class="l">endif</span>; ?&gt;</span>
			
			&lt;/div&gt;
		&lt;/div&gt;
		<span class="y">&lt;?php <span class="l">wp_footer</span>(); ?&gt;</span>
	&lt;/body&gt;
&lt;/html&gt;
</pre>
</div>
<p>In our first <code>dynamic_sidebar()</code> call with the ID 3, we get the admin&#8217;s avatar (the default admin user always has the ID of 1 in all WordPress sites), since I&#8217;m assuming this theme is to be used on a site with only one author. If you want you can insert any other image. The content of this default widget isn&#8217;t really anything useful unless you replace the dummy text with something of your own.</p>

<p>The second widget area with the ID 4, we&#8217;ve added a piece of code to fetch a list of recent posts. The WordPress function <code>get_posts()</code> with 5 as argument to <code>posts_per_page</code> returns five post objects, which we loop through. If you recall the Loop which we&#8217;ve used in all of our templates, we use <code>the_post()</code> to set up the post object in order to use the simpler functions <code>the_title()</code>, <code>the_permalink()</code> and so on to get the post information. We can&#8217;t use <code>the_post()</code> here in our footer to set up the post object because it will conflict with the <code>the_post()</code> in our content. Instead we use <code>setup_postdata()</code> which does the same as <code>the_post()</code> but will not conflict with our other Loop. In later lessons I will explain the Loop and custom queries closer.</p>

<p>The third widget area with the ID 5, we do what we did in our hard-coded menu element in our sidebar. We fetch a list of all WordPress pages with <code>wp_list_pages()</code>.</p>

<p>If you refresh your site and look at your footer, you should see all widgets in action. Check and see if they work! However they will appear beneath each other, because we haven&#8217;t added any styling to footer widgets yet. Let&#8217;s do that.</p>

<h4 style="font-size:13px;">WordPress Codex Function references:</h4>
<ul>
<li><a href="http://codex.wordpress.org/Function_Reference/dynamic_sidebar" target="_blank" title="Wordpress Function Reference: dynamic_sidebar">dynamic_sidebar</a></li>
<li><a href="http://codex.wordpress.org/Function_Reference/wp_list_categories" target="_blank" title="Wordpress Function Reference: wp_list_categories">wp_list_categories</a></li>
<li><a href="http://codex.wordpress.org/Function_Reference/wp_list_bookmarks" target="_blank" title="Wordpress Function Reference: wp_list_bookmarks">wp_list_bookmarks</a></li>
<li><a href="http://codex.wordpress.org/Function_Reference/get_avatar" target="_blank" title="Wordpress Function Reference: get_avatar">get_avatar</a></li>
<li><a href="http://codex.wordpress.org/Function_Reference/get_posts" target="_blank" title="Wordpress Function Reference: get_posts">get_posts</a></li>
<li><a href="http://codex.wordpress.org/Function_Reference/the_title" target="_blank" title="Wordpress Function Reference: the_title">the_title</a></li>
<li><a href="http://codex.wordpress.org/Function_Reference/the_permalink" target="_blank" title="Wordpress Function Reference: the_permalink">the_permalink</a></li>
<li><a href="http://codex.wordpress.org/Function_Reference/wp_list_pages" target="_blank" title="Wordpress Function Reference: wp_list_pages">wp_list_pages</a></li>
</ul>

<h3>Styling the footer widgets</h3>
<p>In the <a href="http://cappelendesign.no/?p=1497" title="Read Lesson 6: Styling our Theme with CSS">previous lesson</a> we styled the sidebar and the widgets with the class <code>sidebar-box</code>, but we haven&#8217;t added any styling for the class <code>footer-box</code> which wrap all of our widgets in our footer. Let&#8217;s add some styling in our <code>style.css</code>:</p>
<h4 style="font-size:13px;clear:both;">style.css:</h4>
<div class="wp_syntax" style="height:350px;overflow:auto;">
<pre class="css">
<span class="g">/*** FOOTER ***/</span>
#<span class="l">footer</span> { <span class="l">color</span>: #dee9f5; }
#<span class="l">footer a</span> { <span class="l">color</span>: #85bce7; }
#<span class="l">footer a:hover</span> { <span class="l">color</span>: #a7d4f7; }

#<span class="l">footer .footer-box</span> { 
	<span class="l">width</span>: 225px;
	<span class="l">float</span>: left; 
	<span class="l">margin-right</span>: 50px;
}
#<span class="l">footer .footer-box:last-child</span> { <span class="l">margin</span>: 0; }

#<span class="l">footer h2.widgettitle</span> { 
	<span class="l">font-size</span>: 16px; 
	<span class="l">font-family</span>: Arial, Verdana, sans-serif;
	<span class="l">padding-bottom</span>: 5px; 
}
#<span class="l">footer .footer-gravatar</span> { 
	<span class="l">float</span>: left; 
	<span class="l">margin</span>: 5px 10px 5px 0;
}
#<span class="l">footer p</span> { <span class="l">line-height</span>: 17px; }

#<span class="l">footer ul</span> {
	<span class="l">list-style-type</span>: none;
	<span class="l">line-height</span>: 16px;
	<span class="l">padding-top</span>: 3px;
}
#<span class="l">footer li</span> {
	<span class="l">background</span>: url(<span class="r">"images/dot-space-invader-light.png"</span>) no-repeat top left;
	<span class="l">padding-left</span>: 17px; 
	<span class="l">padding-bottom</span>: 5px;
}
<span class="g">/* Menu in footer */</span>
#<span class="l">footer ul.footer-menu li</span> {
	<span class="l">background</span>: none; <span class="l">padding-left</span>: 0; <span class="g">/* Remove defaults */</span>
	<span class="l">float</span>: left;
	<span class="l">width</span>: 80px;
	<span class="l">margin-right</span>: 30px;
}
</pre>
</div>
<p>This CSS code takes care of positioning the three columns next to each other, styles the widgets and some additional styling for our default fallback widgets &#8211; such as the menu and positioning the avatar. You should already have the image <code>dot-space-invader-light.png</code> in your <code>images</code> folder, which was included in the image zip provided in <a href="http://cappelendesign.no/?p=1497" title="Lesson 6: Styling our Theme with CSS">the previous lesson</a>.</p>

<div class="wp-caption alignleft" style="width: 210px; margin-top: 10px;"><a href="http://www.cappelendesign.no/wp-content/uploads/misc/wp-theme-tutorial/screenshot4.jpg" target="_blank" class="shutterset_set_wptutlesson7"><img title="Our footer with three widgets" src="http://www.cappelendesign.no/wp-content/uploads/misc/wp-theme-tutorial/thumbs/screenshot4.jpg" alt="" /></a><p class="wp-caption-text">Our footer with three widgets</p></div>

<p>Save, refresh, scroll to the bottom and take a look at our footer. If you have used the same code and styling, and there are no widgets placed in any of the three footer widget areas, this is what we should have now. If you place widgets into one of the footer widget areas it should come in the right wrappings and style nicely.</p>

<p><em>This concludes Lesson 7 of the <a href="http://cappelendesign.no/?p=1229" title="WordPress Theme Tutorial in 14 Lessons">WordPress Theme Tutorial</a>. Go to the next lesson, <a href="http://cappelendesign.no/?p=1654" title="Lesson 8: Post Thumbnails">Post Thumbnails</a>, or back to the tutorial&#8217;s <a href="http://cappelendesign.no/?p=1229#toc" title="Table of Contents of WordPress Theme Tutorial in 14 Lessons">Table of Contents</a>.</em></p>]]></content:encoded>
			<wfw:commentRss>http://cappelendesign.no/2011/10/wordpress-theme-lesson-7-defining-multiple-widget-areas-and-customizing-default-wrappings/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

