Useful WordPress Functionality
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 versitile 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
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 seperating 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.
1 2 | <?php query_posts('category_name=reviews&showposts=2'); ?>
<?php while (have_posts()) : the_post(); ?> |
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 seperate 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:
1 2 3 4 5 6 7 | <?php $temp_query = $wp_query; ?>
<?php query_posts('category_name=software&showposts=4'); ?>
<?php while (have_posts()) : the_post(); ?>
<UL and LI items go here>
<?php endwhile; ?>
<?php $wp_query = $temp_query; ?> |
In case it’s not obvious, in between the list items you would make anchors for <?php the_permalink(); ?> and <?php the_title(); ?> 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 seperate your content by category or page. If you have any useful tips, or find these useful, please let us know!
Ok, this is just spooky. I recently bought my own domain, and have been working on a design since, and have been stuck at the “converting into a dynamic blog” step forever because I was in the same exact boat as you. I will experiment with what you’ve posted here.
Thanks! You have no idea how much this excites me!
Fantastic! I’m glad the info helped. Also, if you have any further questions, please feel free to contact me via my contact form or email – I’d be glad to help out in whatever way I can.
As for the entry, I didn’t have time to right up a full-blown tutorial, but I have a few more tips that I think will get people moving in the right direction towards using WordPress as a powerful CMS.
Thanks that was really interesting, just thought i would point out though your main page is not xhtml 1.0 strict you have one error.
No problem Leckie, I’m glad you found the write up interesting. As for the page not validating, that’s fixed now. I am having a hard time getting WordPress to validate my code tags. I simplified the example a little bit, that made it valid again.
Thanks for the heads up!
No problem just thought you would have wanted to know.
After your writeup im thinking about using wordpress myself.
Is that easier than simply having a template for each category, ie, cat1.php, etc, and having custom output for each cat template?
I’ve tried that and could never get it to work the way I needed it to. I’m sure it’s a great way to do it. Care to elaborate a little bit?
I understand that if you have a category, say “tutorials” that is category 7, you could create a template for each category. The problem I’ve had with this is the priority WordPress puts on different templates.
It’s a hierarchy that I haven’t had a chance to mess with, but would love more information on. In my opinion, the codex is fuzzy on this topic.
Hiya Matt,
Just googled this subject and found you! Very nice looking site btw :-)
I am wanting to use WordPress 2.0.4 as more of a CMS in just the way you describe but I am fairly new to WP and PHP and was wondering if you could detail which template files you insert the various code calls into (ie query_posts etc).
Sorry if I haven’t understood you already!
Thanks in advance.
tim0fee
UK
tim0fee -
That’s the beauty of these functions, they can go in any template file at any time and call in the data you want. For example, you could put the code inside your index.php file and it would call in the specified information from the database.
Does this make sense? Let me know if it’s still an issue.
Hi Matt,
This was exact solution i was finding since couple of days. I am designing a website for an English Language School, And i was very much keen about using wordpress as a CMS. But i was stuck in the requirement of presenting links of different categories and supages on other web pages of the site. U mad my day. Thanks alot for sharing this article.
Regards
How do you get your header to display different content for each page selected? I noticed when I click Home, About, Work, etc. your content changes in the sub-header. Is that part of WordPress or just a static thing built into your site’s XHTML code?
Thanks for the comment Nissa, hopefully this response will help.
The header is controlled with a mixture of custom PHP variables and WordPress code. If you have created a page in your admin section (say “work” for isntance), highlighting that page in the navigation is as easy as writing:
<li class="work"> <a <?php if (is_page('work') | $thisPage=="work") echo('class="active" '); ? rel="nofollow">href="<?php bloginfo('url'); ?>/work/"><span>Work</span></a> </li>The
(is_page('work'))bit is a WordPress call and the$thisPage=="work"part is a custom PHP variable that’s used at the top of the page to ensure the correct placement.Does that make sense? Hopefully that helps, but let me know if you need further instruction.
I’ve been found the same problem like you. And I also look up the answer on the internet and wordpress Docs.
Finally I use codes like below to solve the problem:
have_posts()) : $my_query->the_post(); ?>
Thanks for sharing dude!!! I like very much your blog design. Best regards from Mexico.
This was a really interesting read. I have been working with WordPress for the last couple of months and I wanted to do something very similar on the front page of my website, But I ended up just using the archive pages as my pages to display from each category.. I’m going to go back through my site right now and use your codes.
Thanks man.
Very helpful, thanks for this one Matt. One thing I find myself using more and more are WordPress Custom Fields. They allow clients to control elements outside the realm of the WYSIWYG area. This is also very handy for those situations when you choose not to hand over full WYSIWYG control.
Thanks for the great article, keep the WordPress CMS stuff flowing!
Thanks for this.
Quick question, how do you make all the boxes the same size on http://www.45royale.com/all/articles/
Thanks.
i found you from smashingmagazine, and, wow !! very excited !!!! thanks Matt. It`s very useful
Handy tip, thanks!
Here’s a dynamic version I came up with for a site I worked on:
It pulls posts from any category that exactly matches the current page’s name.
Cheers!