Useful WordPress Functionality
Note: This article is very old and I’m leaving it here mostly for posterity. There are some basic concepts here that still apply, but you should probably ready up on the WordPress Loop in it’s current iteration.
The holidays are over and it’s time to get back to business. I’ve been extremely busy here at the shop and I am grateful for all the new client work headed my way. Although most of the work has revolved around web design, I’ve also noticed that more and more clients are requesting a CMS for their site.
There are many out there, from Moveable Type, to Textpattern, to Expression Engine and plenty more that I haven’t mentioned. On this site, I use WordPress and have found it far more versatile than I ever thought possible. However, it has taken some time to get it working just the way I like it, so I figured I’d post a few of the solutions that I’ve stumbled upon with the hopes of helping some folks out with problems they’ve encountered.
Using “Pages” to display specific category posts in WordPress
Recently, I have been developing the idea of having each section of a site controlled by the same database and structure as the rest, but operating a little bit differently in regards to post display. By this I mean separating the standard “blog” posts from the “newest features” or “reviews”, yet writing and maintaining all of these articles in one database. I wanted to stretch WordPress a bit farther and utilize it for every aspect of my content management within the site. So I had to find a way of structuring the code such that “The Loop” was displayed as certain conditions were met, in this case based on which category was being called upon. I set up my Pages within the site and went to town.
I scoured the WordPress Codex for information, but everything ended up having to do with excluding categories from lists or only displaying certain current posts. Not what I wanted. I wanted to be able to have each Page of the site represent a different category and only display posts from that category. So the search continued. Finally, a few posts began to marinatein the old brain and I began forming an idea of how I would go about this. What I came up with was a query to the database based on certain parameters; meaning that I would have total control over what was displayed and seen on any given page. I set up custom templates and used this code before every post to call the content that I wanted.
In short, this code sends a query to your WordPress database asking for the contents of a specific category. In place of category_name=”reviews”, you would give the specific title of your category such as css, portfolio, unicorns, whatever. Once the category has been assigned, you then tell WordPress how many posts you would like to display. In this case, I make a call to the “reviews” category, asking it to return the two most recent entries with that category ID. The second line of the code is standard WordPress post material; if there are posts to return show them with the quantity specified.
This simple query can help you separate your pages out into different sections and control the content of your site while still giving you the dynamic results you are looking for.
Let’s take that concept one step further
So you now know now to set up a custom template, call a specific category by query and display only posts that you want. But what if you want to show an abridged list of recent content in a specific category elsewhere on the website, all the while keeping the posts specific to a designated category. No problem. You simply create a temporary query and call the category name and number of posts you want to show to an unordered list that you can then style however you like. The code for this is as follows:
UL and LI items go here
In case it’s not obvious, in between the list items you would make anchors for
to show the permalink and title of the last 4 posts entered in that category. If you think about it, this concept can be extrapolated throughout your entire site and used over and over again to display snippets of relevant data wherever you please. Maybe you want a home page with “headlines” of categories 1-6. Using variations of the code above, you could tell WordPress to search for different posts in different categories and display them all on nice and neatly on the same page. Once you know how to apply the concepts, the rest is left to your imagination.
I’m sure I am not the first one to use these in their WordPress CMS, but it is definitely something that comes in handy when you are trying to separate your content by category or page. If you have any useful tips, or find these useful, please let me know!