Welcome to Wordpress for Designers Day 7! We’re moving right along with our custom Wordpress theme today and will be creating a few more files from scratch. We will also finish making our sidebar completely widget ready and have a look at what that means and how it works. Get your text editor and local server ready and let’s go!
Happy Valetines Day everyone! We are back and ready for Wordpress for Designers Day 6 and moving right along with where we left off in Day 5. Today we will continue to split up our theme files in order to allow us more flexibilty when working with our theme. We will also have a look and discuss the Wordpress template hierarchy. In addition, we will be creating and managing our single.php files, which will help us format our individual posts. Let’s get going!
Day 4 is here, and we’re jumping right back into creating our Wordpress theme from scratch! In this episode, we will have our first look at what is known as the Wordpress loop. We will be using the WordPress loop as well as some handy template tags to start pulling out and displaying content from our database. Lets get started!
Many multi-author blogs have an ‘About This Author’ section attached to the bottom of each post – containing a link to the author’s site and a short Bio about them. In this post, we’ll be creating this, and also an ‘Authors Archive’ page just like the one at NETTUTS:
Considering WordPress’s popularity and huge user base as a blog platform, it makes perfect sense that people are pushing and testing its limits. I’ve been seeing creative and new uses for WordPress everywhere. Photo galleries, job posting boards, e-commerce sites. The list goes on and on. Once you learn what WordPress can do, the sky is the limit.
During a recent project of mine, I needed a way to retrieve the first image from each WordPress post in a certain category, and then scale it to a certain size. I could’ve used a Custom Field for this purpose, and have the author paste the link to the image in a box; however I felt this would only confuse my client. Here’s a look at how it was done. (more…)
It doesn’t seem like that long enough that WordPress announced a redesigned admin panel by usability experts Happy Cog. I remember hearing the announcement and being excited to download the beta and try it out. Now, just a few months later, WordPress is at it again with version 2.7 which is now in its second beta release. Its packing massive overhauls, visual and functional.
I’ve heard a lot of people online complain that while WordPress is easy to learn at a basic level it can be hard to unlock some of its more complex features. When I first was learning to make WordPress themes, I found myself stuck making only "blog style" themes. You know what I’m talking about: a basic page with a header, a navigation, a long section of posts, a sidebar, and a footer. I was relatively happy with them and even today that style still serves a definite purpose.
But sometimes you have more than one type of content to present. You may want a large, featured post area for showing off recent work. You may want more than just a sidebar recapping your recent posts, perhaps a sidebar with actual, fresh content in it. This post will hopefully shed a little light on an easy way to gain control of your WordPress content. Using “query_posts” to pass parameters to your WordPress loop will let you put any content any place in your layout.
I feel a quick warning is in order. This post is a little bit wordy and is intended for users who are already somewhat familiar with WordPress.
Below is the example I’m basing this article on (and it just so happens to by my personal blog). I’m sure there are many other great examples using query_posts but since I can’t see their code I chose this one to make sure it was a valid example.
A quick glance shows you that I have multiple types of content. I have a space to show recent work (it slowly fades between the three most recent entries in the category "templates"). I have a space to show my recent blog posts (it shows the ten most recent entries in the category "posts"). In the first sidebar, I have a section where I post short reviews of movies I’ve recently seen (shows the seven most recent posts in the category "movies"). In the second, smaller sidebar, I have a space for a mini design showcase (it shows the five most recent posts from the category "design").
Most themes use The Loop for getting the posts from the WordPress database. If you have made a WordPress theme, you are probably familiar with The Loop. Along with The Loop, query_posts is an extremely useful tool. Here is what WordPress.org’s codex has to say:
"Query_posts can be used to control which posts show up in The Loop. It accepts a variety of parameters in the same format as used in your URL (e.g. p=4 to show only post of ID number 4). "
Query_posts is placed right before The Loop and will change what posts The Loop retrieves. For example, if you wanted to only call posts from the category "movies" your query would look like this:
<?php query_posts('category_name=movies'); ?>
and you would place it right before your Loop. The whole thing would look like this:
<?php query_posts('category_name=movies'); ?> <?php while (have_posts()) : the_post(); ?> ...do stuff... <?php endwhile;?>
"…do stuff…" is of course where you would put your template code. Things like the_title and the_content and your layout. Now lets get more specific. Say, for example, if you wanted only the most recent five posts from the category "movies" that had the tag "comedy" you could use this query:
<?php query_posts('category_name=movies&tag=comedy&showposts=5'); ?>
You can tack on as many parameters as you need. This allows you to have complete control over any bit of content in your database. You can create layouts and not really have to worry about how you will get WordPress to stick each piece of content where you need it. It allows you to be more creative with your layouts and with your content. In my blog’s current layout, I would never want those mini movie reviews to show up as a regular post; they are far less significant. Displaying them in a smaller, sidebar sized area works much better for the layout.
Using this technique you can achieve very complex "magazine" style themes, CMS style themes, professional business themes not geared towards blogging, and anything else you can dream up. You can even use it to display single items. Say you had a client who wanted his contact info listed at the top of this theme. Its just a few lines of static information, right? Normally you would just insert this directly into one of the template files. Instead, using query_posts you could call up only a single post that is in a category "info" and is tagged "contact." The query would look like this:
<?php query_posts('category_name=info&tag=contact&showposts=1'); ?> <?php while (have_posts()) : the_post(); ?> <?php the_content(); ?> <?php endwhile;?>
Then if the client needed to change his contact info, he could just edit a post and not have to worry about digging into the templates. Calling posts in this way makes your entries extremely flexible. You could use the "info" category to hold all sorts of info: a short bio post, a introduction paragraph, a message for your footer, or a quote from a client. Then you could call those specific posts anywhere you’d need them. It really allows you to have an extra way to enter, store, and display content.
If you use query_posts around each Loop you use, you don’t have to worry about these extra posts showing up anywhere else. If you still are using a regular loop you may want to exclude the "info" category, and you could do so with a query like this:
<?php query_posts('cat=-3'); ?>
where "3" is the ID of the category "info." Notice there are many different category parameters (like category_name, cat, category__and, etc). For a complete list, read the query_posts page on the WordPress codex. While learning how to use query_posts you’ll want to keep this page open for reference and to see what its really capable of.
There is one down side, though, but its not a major one. When templating in this way you end up with certain category names, category IDs, post tags, etc hard coded right into your theme. If its a job for yourself or for a client, this is just fine (since you are the one setting up all the categories and posts anyway). But if you are aiming to use it in a template for resell, you’ll need to include full explanation and explain which categories need to be created and how to use them correctly make use of the theme. Of course, if you are more advanced, you could use a WordPress options page with your theme and let the user handle some of the setup. Dan Harper has an excellent write up about theme options page you can read here.
Once you’ve mastered query_posts and its parameters you’ll find templating with WordPress will be much, much easier. Your templates will be more flexible, dynamic, and organized. You’ll have complete control over every bit of content and will be able to switch it all out at a moment’s notice, without having to touch your template files. Its a great benefit to any theme for a client that might not be too savvy, but its also just a great way to setup your own blog or your next template for sale.
Including a Theme Options page for your theme is one of the best ways to increase ease-of-use for managing a complex theme. However, a few quick Google searches later and most people give up. Such a great inclusion for theme design appears to have such little documentation, that it appears to be one of those heavily guarded secrets which only the crème de la crème of designers hold the key to.
In this article we will be incorporating an options panel for the ‘WordPress Classic’ theme. The methods you learn will allow you to very easily integrate it into an existing theme you’re working on.