How to Create a Homepage With Multiple Listings Using Custom Queries

WordPress gives you a couple of built-in options for your website's front page: a display of your most recent blog posts or a static page of your choice. But what if you want to create something a bit more interesting than that?

If your site has a lot of data using categories, taxonomies or custom post types to organize it, you might want to display data of more than one kind on your homepage. The good news is that you can do this using a custom template for the homepage. In this template you write a number of custom queries to list the data in the way you want to. In this tutorial you'll learn how to do the following:

  • Create a custom homepage template for use by your theme
  • Include four queries in that template to display different kinds of data, including three custom queries

The queries will list standard posts plus one custom post type registered for this project, called 'animal'. You can find the template file with the four queries in the code bundle.


What You Will Need to Complete This Tutorial

  • A development installation of WordPress
  • A theme installed which you can edit. If you're working with an existing theme I recommend making a child theme so you can create template files without editing the parent theme. I'm using a child theme of Twenty Twelve

The Homepage Design

Before starting to set up the homepage template, first take some time to identify what's contained on the homepage:

Front page design with header

The homepage contains:

  • A header with a banner and navigation menu
  • The homepage content - text and an image
  • Three listings, each using a custom query:
    • Updates - these are standard posts in the 'Updates' category
    • In Detail - standard posts in the 'Detail' category
    • Animals - these use a custom post type called 'animal'
  • A standard footer

The elements of this layout you will be working with is the homepage content and the three listings.


Before You Start: Setting Up the Data

Before starting, you will need to have a few items in place for your queries to work with:

  • An 'animal' custom post type registered. For details of how to do this, see register_post_type in the WordPress Codex. Alternatively, the functions.php file in the code bundle includes the code which does this
  • Two categories set up for normal posts - 'Updates' and 'In Detail'
  • A static page set up called 'Home', with some content added to it. This should be set up as the homepage via the Settings page in the WordPress dashboard
  • A few normal posts in each category and a few animals (using the custom post type). The template will display up to eight of each

Creating the Front Page Template

The first step is to create a template file to hold your code. Create a blank file in your theme folder and name it front-page.php. Add the following code to the file:

Save your file. As you can see, this includes the header and footer template files on your homepage. No sidebar is included as that's not part of the homepage design. When you visit your site's homepage you will now have a blank page with just the header and footer displayed:

blank home page with header and footer, no content

Adding Static Content to the Homepage

As well as showing the results of custom queries, your homepage will display the content from the 'Home' static page. To add this to your template file, insert the following code to replace the '// front page content goes here' line:

This is the standard loop. WordPress will use it to display the contents of the static page you've registered as the homepage. Your homepage will now contain the static content:

the home page with static page content

The First Custom Query With WP_Query

For the custom queries, you will use the WP_Query class. This lets you set up custom queries and run a loop based on each one to display posts.

Using WP_Query is much better practice than using the query_posts function as the latter completely resets the main query and can cause performance issues. WP_Query takes a set of parameters which define the query you want to run. Some examples of the parameters you could specify include (but are not limited to):

  • post_type - posts, pages, attachments or a custom post type
  • category
  • tag
  • taxonomy term(s)
  • posts_per_page - the number of posts to display
  • order - ASC or DESC
  • orderby - you can order by date, name, author, menu_order or many more parameters
  • ...and many more

For details of the parameters you can use with WP_Query, see WP_Query in the WordPress Codex. To use WP_Query, you insert the following code:

On your homepage you want to list the last eight posts in the 'Updates' category. To do this, add the following code between the main query and the call to the footer:

This creates a <section> element to contain your listing and opens the listing with a link to the category archive in an <h2> tag. It then defines the arguments for WP_Query and runs the loop based on that query. The additional classes can be used to style each individual listing.


The Second Custom Query

The second custom query is almost identical to the first, it just queries posts from a different category. Add the following after the first query and immediately before the second closing </section> tag:

This adds a second post listing for posts in the 'In Detail' category.


The Third Custom Query: Querying a Custom Post Type

The third and final query doesn't query standard posts, but the 'animal' custom post type you registered before creating your template file. After the second custom query, add the following:

You now have all of the queries in place and displayed on the home page:

the home page with all queries added

Style the Listings

The final step is to add some styling. At the moment the listings are one below the other, with very little in the way of spacing or decoration.

In this step you'll add some simple styling to improve this. In your theme's stylesheet, add the following:

This will ensure that the listings are cleared below the main content, and place them side by side. It also adds some spacing between the items in each list:

the home page with the listings styled

Summary

You now have a customised homepage which includes the static page content plus three custom queries listing different content types. You could adapt this approach to different requirements. For example:

  • For a front page which just shows archives and no static content, simply remove the first loop
  • To list the 'animals' post type in a different way, you could specify taxonomies in your arguments for WP_Query
  • You could add or change the content displayed for each post, for example using the_thumbnail() to display post thumbnails (or featured images)
  • You could add custom queries to your sidebar or footer template to display them on all pages

Resources

Some useful resources:

Tags:

Comments

Related Articles