Different Ways To Display Content in Wordpress
Author: Stefan Vervoort | Please Comment!While I was developing the new theme for DivitoDesign (which will knock on your doorstep soon enough), I was facing some issues about how to display content with Wordpress as I am not going for the traditional look with this theme, but with a Magazine look.
The power of magazines or magazine looking blogs are the loads of information that can be displayed. If you want to run a successful magazine, you need to focus on writing content and not on manually add the entries to your theme. An important factor for a magazine is there how you automatize the process of displaying content.
We will use the Wordpress template tag query_posts();. Remember this tag only works in the WP loop. You can find a version of the WP loop I use below:
<?php query_posts(); ?> <?php while (have_posts()) : the_post(); ?> <div class="post" id="post-<?php the_ID(); ?>"> <h2><a href="<?php the_permalink() ?>" rel="bookmark" title="<?php the_title_attribute(); ?>"><?php the_title(); ?></a></h2> <div class="entry"><?php the_excerpt(); ?></div> </div> <?php endwhile; ?>
Parameters for query_posts()
We are going to give the query_posts() tag some parameters on how to operate. Some of those parameters are given below:
- cat > the category you would like to choose
- showposts > the number of entries you would like to show
- offset > number of the entry you don’t want to display
- orderby > order the entries by author, category or anything else
- tag > select entries by tag
- More parameters can be found at Wordpress
Display Featured Post
In a magazine theme you might want your visitors to focus on the featured entry. You need to add your specific entry to a category named featured.
If you want to show one featured entry in the loop, change:
<?php query_posts(); ?>
to:
<?php query_posts("cat=featured&showposts=1"); ?>
Latest Post
If you want to select the latest entry, this parameter will get you there:
<?php query_posts("showposts=1"); ?>
The other posts
If we have selected the latest entry from the database, you might want to show the other (older) items as well expect the ‘latest’ item you have already shown. Here’s the query:
<?php query_posts("showposts=5&offset=1"); ?>
Exclude Category(s) or tag(s)
If you want to display all entries, expect the ones from a certain category or tag you need to get to the ID of the category or tag first (wp-admin>manage>tags/categories):
<?php query_posts("cat=-1"); ?> <?php query_posts("category__not_in=2,3,4"); ?> <?php query_posts("tag=-2"); ?>
Show posts by Author
It is easy to make a page that shows all the posts a specific author wrote for your blog:
<?php query_posts("author=2"); ?> // Author's ID (wp-admin>users) <?php query_posts("author_name=stefan"); ?>
Examples
These examples are just a couple things I have learned lately and it’s not everything that’s out there. You should remember there is alot more you can do with Wordpress Template tags, as well as with the query_posts() attribute.
For more examples visit the query_posts codex page.










Loading...
9:00 pm on September 19th, 2008
Great tutorial getting at the guts of a Wordpress template. I’m just getting in to creating and modifying Wordpress templates, so this post will come in handy as a reference. I’m going to Digg it!
9:09 pm on September 19th, 2008
I’m also planning a restructure of information over at my site, and like you, I’m using Wordpress as my weapon of choice.
There does seem to be a shortage of worked examples on using custom methods (i.e. not the traditional loop) to pull content onto a page, so thanks for this!
10:27 am on September 20th, 2008
@Bottlecap & Stuart - Great this guide helped you, and thanks for your comments!
12:43 am on September 21st, 2008
Thanks Stefan! Just what I was looking for today.
5:12 pm on September 21st, 2008
Thanks for this tutorial. I have been wondering for a long time how to do this. I will use this information. Thanks again.
6:30 pm on October 1st, 2008
nice work out tahnk for this info
8:53 am on October 2nd, 2008
Thanks for those comments and links my friends!
7:43 pm on November 4th, 2008
This was a usefull info. Thank you for the list. I’m getting to know the WP one day and will sure use these methods.
7:04 pm on January 1st, 2009
This was good info cause I’m trying to fix a new blog to show posts from different authors. Is there a way to show the posts from oldest first and new one last? That would be a great way to read a blog from the beginning and move towards the end. Like a diary from the start