Views plugin lets you display fields of parent pages by setting the ‘id’ attribute. But how can you display fields of grandparents, or their ancestors? We explain how to achieve this using Content Templates and reach as deep into the post hierarchy as we wish.
Example – errata for specific plugin versions
We take this example from our very own sites. The new ‘errata’ content displays known issues and workarounds for plugins. Each errata is created for a specific plugin version.
In the database, the content is connected as:
We want to create View, which shows erratas. For each errata, we need to display the version (parent) and download (grandparent).
Reminder – how to display parents
If we wanted to display fields of the parents, we’d need to add the ‘id’ attribute to shortcodes of these fields.
For example, to display the ‘title’ field of the ‘version’ of the errata (version if the parent of the errata), we’d use:
This is covered in the tutorial on displaying fields of parents. However, since we don’t have a direct syntax for specifying deeper than the parent, we need to do something else to display the fields of grandparents.
Solution – using Content Templates to switch the ‘context’
Our solution is based on the fact you can set the post when displaying Content Templates. When a page is displayed, the ‘context’ of that page is the current post. Views plugin lets us display only the parents of the current post. So, if we display a Content Template, inside that page, with the context of a parent, we can access grandparents fields inside that Content Template.
Follow this example to see exactly how we apply this idea and display fields of the grandparent ‘download’, on the ‘errata’ page.
- Use a Content Template to display the parent (version) information.
- Inside this Content Template, use the ‘id’ arguments to access its own parent (download), which is the grandparent of ‘errata’.
Into our View, we insert a new Content Template. Use the Views GUI to create a Content Template and edit it:
|1. Insert a Content Template for the parent information||2. Create the new Content Template and insert its shortcode||3. Add ‘id’ to the Content Template to display parent fields||4. Edit the parent Content Template and add grandparent fields|
The resulting shortcode, which we inserted into the View is this:
[wpv-post-body id="$version" view_template="errata-version"]
Our new ‘parent’ Content Template, displays the fields of the ‘version’, which is the parent of the errata. Now, we can access fields of its own parents. To do this, we insert all fields and add ‘id’ attributes to those that belong to the grandparent post.
You can see the Content Templates that we’ve created, in the Erratas list:
Summary – display fields for any post hierarchy
Using this technique, we can access any depth of post hierarchy and display any field that we need. A Content Template lets us set the context for fields display. Once we switch to the parent content, we can use the ‘id’ attributes to access its parents, which are the grandparents of the original post.
We use a similar technique to display brother pages. For brother pages, we used the WordPress page-parent connections, but the idea is similar. We created a Content Template to switch the context and inside that context we can get page children.