Filter Genesis Sitemap with genesis_sitemap_output, public domain image from pixabay.com

genesis_sitemap_output Filter

Filter Genesis Sitemap with genesis_sitemap_output, public domain image from pixabay.com

One feature of the Genesis Framework is the ability to easily create an archive page. Create a new page, assign the Archive template to it, and you’re done. When you add it to a menu, the archive page displays links to all pages, categories, authors, monthly archives, and your last 100 posts.

Archive pages are sometimes referred to as sitemaps, but please don’t confuse this type of sitemap with the XML sitemap that you register with the search engines to help index your site. While both of these sitemaps help your SEO and indexing, they are distinct tools and output their data very differently.

As with all things, times they are a-changin’, and the release of Genesis 2.2 changed a lot. For example, examine the old and new page_archive.php source codes displayed below. In the “Out with the Old” section, page_archive.php had all of the code within the template to generate the sitemap. You’ll see the bulk of this code in the genesis_page_archive_content() function (highlighted).

In the “In with the New” section, you don’t see the archive code at all. What you see is a new function called genesis_sitemap. It is this function that now drives the content of archive pages. This function is found in genesis/lib/functions/general.php. For your convenience, the source code may be viewed below. But notice something magical at line 40—that’s right, it’s a filter! While it’s beyond the scope of this article to fully explain filters, I will say that they make it easy to change how a function works.

Out with the Old

In Genesis versions earlier than the 2.2 release, the page_archive.php file contained:

https://gist.github.com/vfontjr/987e017a59e69992e070ad57e5678f5c#file-page_archive_old-php

You’ll find tutorials all over the Internet about how to change this version of the page to customize your sitemap’s output. For example, suppose you don’t want the author archives to display, you would comment out lines 39 through 44 as I’ve done in the above example.

In with the New

Since the release of Genesis Version 2.2, the new page_archive.php contains:

https://gist.github.com/vfontjr/987e017a59e69992e070ad57e5678f5c#file-page_archive_new-php

Much simpler, isn’t it? While the genesis_page_archive_content() function hasn’t shown up on the deprecated list yet, its output content has essentially been replaced by genesis_sitemap() in Version 2.2. So the question becomes, “How do I remove the author section now?” Genesis_sitemap is a core function that should never be changed, but remember the filter? Let’s jump ahead to the Filter Genesis Sitemap with genesis_sitemap_output section to learn how.

genesis_sitemap()

https://gist.github.com/vfontjr/987e017a59e69992e070ad57e5678f5c#file-genesis_sitemap-php

Filter Genesis Sitemap with genesis_sitemap_output

To remove the author content from the new page_archive.php, we need to add a new filter to the child theme’s functions.php. I’ve chosen not to pass any parameters to my new function. In the genesis_sitemap function, the heading parameter is passed. In this new function, I’ve decided to determine the proper headings for the archive page by including the same code that is used on line 36 in the code above. I’ve also copied the content from genesis_sitemap into my new function. Here I can comment out the author archives (lines 12 through 15).

https://gist.github.com/vfontjr/987e017a59e69992e070ad57e5678f5c#file-vmf_sitemap-php

The are other ways you can use the genesis_sitemap_output filter as well. In the first example, you can modify the text that displays as headers. In the second example, you can replace the sitemap altogether with a WordPress function.

Example 1

https://gist.github.com/vfontjr/987e017a59e69992e070ad57e5678f5c#file-example1-php

Example 2

https://gist.github.com/vfontjr/987e017a59e69992e070ad57e5678f5c#file-exaample2-php

See? I told you it was magic.