Taxonomy Term List Tutorial

Many users have been asking how to add a list of taxonomy terms to their app and display the corresponding post lists. In this tutorial we’ll see how to achieve that and, by extension, how you can implement any post search based on taxonomies.

Along with the main taxonomy terms topic, this tutorial is also a good way to dive into the following key notions of WP-AppKit theme customization:

  • Create a custom page template
  • Create an app component dedicated to post search
  • Customize app history and screen transitions management

If you’re ready, let’s see how to implement this nice feature 🙂

List Taxonomy Terms

First we’re going to build the page that will display the list of our taxonomy terms.

For this example we will use a taxonomy “Category” that has 3 terms “Blog”, “Snippets” and “Uncategorized” but the same would apply to any custom taxonomy and any number of terms of your own.

WordPress Side

Create a new WordPress page “My categories” (or any custom taxonomy name of yours) and publish it. You don’t need to set content for it (except if you need this content in the app) as it is not meant to be displayed on site’s Front-End, just in the app.

Then add this page to your app, as a component of type “WordPress page”.

"My categories" page component

One important thing : for the code in this tutorial to work, the page component’s slug must be “term-list”, so that we can target it in the app. To set the component’s slug, find it in the components list, click the “Edit” link and edit the “Component slug” field:

Also add this component to your app’s menu so that you can navigate to it in the app.

Now if you preview your app, you can see that your page is there, but it does not display any term list yet. Let’s see how to attach term list info to the page’s data sent to the app.

App Side

Add Terms to the Page

In your WP-AppKit app theme (here we’ll use “Q for Android” theme, located in /wp-content/themes-wp-appkit/q-android, but you should work on your own copy of this theme), go to the “php” folder (or create it if it does not exist) and add a new php file “my-taxonomy-terms.php” that contains:

Taxonomy terms are now added to the page’s data, so that we can retrieve and display them on app side.

Add a Custom Template for the Term List Page

Now let’s create a custom page template in the app to display the list of terms:
Create a “page-my-taxonomy-terms.html” file in your app theme folder (/wp-content/themes-wp-appkit/q-android/page-my-taxonomy-terms.html) with the following content:

And tell your app to use this template for your component with slug “term-list”, by adding the following to your app theme’s functions.js:

First set our term list component ID so that we can refer to it in different parts of the Javascript code to come:

Then connect our custom “page-my-taxonomy-terms.html” template:

Alright, now you should see your term list displayed in the app.

"My categories" term list screen

But nothing happens when you click on terms… we should definitely do something about it.

Display the Post List Corresponding To a Chosen Term

When you click on a term in the list, you’ll want to display the posts that correspond to this term. To achieve that, we’ll implement a search query to the server so that it can send us the posts that are linked to this terms, which we can then display as any other post list in the app.

First, add a new component “Search” to your app of type “Post list” with the slug “search“. You can choose “No taxonomy” in the taxonomy field as we will set the taxonomy using hooks in a moment. Don’t add this component to your app navigation, it is just meant to handle our search query, not to be displayed in app’s menu.

Search component

Then call this component’s search when we click on a term in the list, by adding the following to your functions.js file:

And for this to work, we need to add our “current_search” data (ie the taxonomy term that we want to search on) to the the web service that performs the search:

Finally we need to interpret this search on server side, by adding the following to the same “my-taxonomy-terms.php” php file that we started earlier:

Ok, that was a bit of coding, but if you’re still alive click on a term in your app and you should see the corresponding post list display!

Last Adjustment: Fix History

You may have noticed that when you display your post list corresponding to a term, there’s no back button to come back to the term list. This is because the app does not know yet that your post list screen is a “child” (speaking from the app history perspective) of the term list screen. Here’s how to fix that (in functions.js):

And that’s it, we have now a working example of how to display the terms corresponding to a taxonomy and the posts that correspond to each term.

Post list with back button

You can find this same code implemented in the “Q for Android” theme here.

Going Further

One taxonomy is not enough? Or you need to filter your result post list on other criteria? You can build any more advanced search feature on the basis explained in this tutorial. Here are the steps that you can follow to do so:

  • Modify the term list template (page-my-taxonomy-terms.html) to change for example your term list into a select box displayed in a form, along with other taxonomies you want to filter on.
  • Then in functions.js, replace the “link click” binding by a “form submit” binding, and set the “current_search” object according to the filters data submitted in the form.
  • Finally in the “my-taxonomy-terms.php” php file, adjust the “$tax_query” array to your needs.

We hope this tutorial will help you build great taxonomy (and more!) based searches.
Happy app coding, and don’t hesitate to drop a comment for any question or feedback.

Published by Mathieu on July 16, 2018

Freelance Senior Web Developer, WordPress since 2009, JS/Backbone since 2012, Custom Back-End devs, WebApps and Plugins for clients and the community.

Leave a Reply

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

Having questions?

FAQ | Tutorials | Documentation

Or

Contact Us