Search

How to Create a Category Archive Page in Genesis

Listen to this article

archive-page-genesisAn archive, is a collection of items or records. For example, a museum is an archive for antiques, while a library is an archive for books. Similarly, in WordPress, we have archives for post types. For example, you can have archive pages for categories, tags, author posts, event for custom post types. The ‘Blog’ page on your site is also an archive page.

So let’s experiment a bit. Login to your WordPress dashboard, and search for the blog page (this page might not necessarily be named ‘The Blog’, but on your site, it’s the page which has an index to all posts). Moving on. You’ll notice that the blog page does not have any content on it!

So then, how does the blog manage to show post content?

Well, you see, archive pages like the blog, or category or tag archive pages, display content based on queried parameters. For example, when a reader clicks on a category term on your site, WordPress handles it by querying for posts, and filtering them based on the category term. The content is then displayed using a template your theme provides (usually the archive.php).

That’s all okay. The use of a template, makes it simple to uniformly display content. And the advantage you gain, is that you don’t have to explicitly create an archive page for every new tag or category term you add. However! The primary drawback here, is that you do not have control over the display of the content. Neither is there an option to create new archive pages if needed. You have to track down the template file and then change it. But then this method, could cause you to lose your changes on a theme update!

A better solution, is to either create a new page template for archives, or use another option Genesis provides you (which I will be explaining later on). So, say for example, you want to create your own archive page for posts categorized under ‘Genesis Tutorials’. We’ll take a look at how each approach can help us customize this page.

[space]

Create a Page Template for Archive Pages

Let’s begin with the first approach. So what we will be achieving here, is a new page template, that we will have created from scratch. We will then assign this template file to a page, which will be the actual archive page.

Step 1: Create a Template File

Head on over to your theme’s directory, /wp-content/themes/my-theme/ (this would also be the directory of your Genesis child theme). Here, create an empty file page-genesis-tuts.php.

Step 2: Add Structure to the Template File

Here, you can decide what contents the archive page will display, and how the content will be structured. Paste the below code in page-genesis-tuts.php:

<?php
/** This will be the name of the Archive Page Template **/

/* Template Name: Genesis Tutorials Template */

/** We have to remove the default Genesis loop, and add our Custom loop **/
remove_action( 'genesis_loop', 'genesis_do_loop' );
add_action( 'genesis_loop', 'wdm_do_custom_loop' );

function wdm_do_custom_loop() {
/** Here, we have to alter the query parameters which are sent to the loop, so as to display just the posts under category ‘Genesis Tutorials’. You can filter posts by category id (to display posts from a particular category and sub categories), or category_slug (for posts only from the particular category) **/

global $paged;
global $query_args;

$args = array(
/** Set the Category ID. To find this ID, go to Categories and click on the Category link. The URL you see, will contain the category ID, for the parameter tag_ID **/
       'cat' => 4, // assuming ‘Genesis Tutorials’ has the ID 4
       'paged' => $paged, // to maintain pagination
   );

  // add additional content if needed
   genesis_custom_loop( wp_parse_args($query_args, $args) );
}

genesis();
?>

Hint: If you want this template to be set for just this one page, you could name this file page-{ID}, or page-{page-slug} (where ID is the ID, and page-slug is the slug of the page, where this archive will be displayed). For more information read about Specialized Page Templates.

Step 3: Assign this Template to a Page

The final step of course is to assign this template to a page. So create a new page, and set the template as Genesis Tutorials Template.

If this method perplexes you, don’t worry. You have an option. Let’s take a look at how we can create an archive page using custom fields provided by Genesis.

[space]

Create an Archive Page Using Custom Fields

Genesis provides a built-in functionality to create single category blog pages, using custom fields. You, just need to do the following:

Step 1: Create a New Page

Nothing much to explain here. Just go to your dashboard, and create a new page.

Step 2: Assign the Blog Template

Set the template as ‘Blog’.

Step 3: Add a Custom Field

To display an archive page for ‘Genesis Tutorials’, add a new custom field with name ‘query_args’ and set the value as ‘cat=4’ (since 4 is the category ID of Genesis Tutorials). Add more parameters if needed. (If the Custom Field section is not visible, select the visibility from ‘Screen Options’)

custom-field-genesis
Add a Custom Field

Step 4: Publish the Page

And that’s it!

[space]

Simple wasn’t it! However, the primary difference between the two methods, is that in the first method, you have total control over page contents, since you’ll built the entire template. Whereas in the second method,  you have a simple way to create archive pages for post types, filtered by certain arguments. So, the choice you make would depend on your requirements. 🙂 Do let me know if you have any additional questions or feedback for me, using the comment section below!

Aruna Vadlamani

Aruna Vadlamani

Leave a Reply

Your email address will not be published. Required fields are marked *

Get The Latest Updates

Subscribe to our Newsletter

A key to unlock the world of open-source. We promise not to spam your inbox.

Suggested Reads

Join our 55,000+ Subscribers

    The Wisdm Digest delivers all the latest news, and resources from the world of open-source businesses to your inbox.

    Suggested Reads