Skip Navigation

[Resolved] Creating template for displaying relationship hierarchy

This thread is resolved. Here is a description of the problem and solution.

Problem: I have 4 post types related in a series of one-to-many (O2M) relationships - child, parent, grandparent, and great-grandparent posts. On the single child post template, I would like to display a breadcrumb trail from all 4 levels of post relationship hierarchy.

Solution: From any one level of the hierarchy, you can display information about that level and one level of hierarchy higher. So from the child post context, you can display information about the child and the parent. The idea is that you'll have to create 2 nested Content Templates to switch context up the hierarchy chain one level at a time. Delete the microcycle link and the two grandparent templates from the View for now. Create a mesocycle-breadcrumb template and a microcycle-breadcrumb template. First, the mesocycle-breadcrumb template will include the macrocycle template in the context of macrocycle from mesocycle, and the post link shortcode to display the mesocycle link. The mesocycle link shortcode needs no context because we will set the template context in the microcycle-breadcrumb template where we place the mesocycle template:

( in the mesocycle-breadcrumb template)
[wpv-post-link item="@macrocycle-mesocycle.parent"] > [wpv-post-link]

Then, the microcycle-breadcrumb template will include the mesocycle template in the context of mesocycle from microcycle, and the post link shortcode - again, it needs no context because we will set the template context in the View where we place the microcycle-breadcrumb template:

( in the microcycle-breadcrumb template)
[wpv-post-body view_template="mesocycle-breadcrumb" item="@mesocycle-microcycle.parent"] > [wpv-post-link]

Then you'll include the microcycle-breadcrumb template in the View, setting the microcycle context from the session context:

(in the Session View)
[wpv-post-body view_template="microcycle-breadcrumb" item="@microcycle-session.parent"] > [wpv-post-link]

Relevant Documentation:
https://toolset.com/documentation/customizing-sites-using-php/functions/
https://toolset.com/documentation/post-relationships/how-to-display-related-posts-with-toolset/
https://toolset.com/documentation/post-relationships/how-to-display-related-posts-with-toolset/displaying-fields-of-grandparents/

This support ticket is created 3 years, 10 months ago. There's a good chance that you are reading advice that it now obsolete.

This is the technical support forum for Toolset - a suite of plugins for developing WordPress sites without writing PHP.

Everyone can read this forum, but only Toolset clients can post in it. Toolset support works 6 days per week, 19 hours per day.

Sun Mon Tue Wed Thu Fri Sat
8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 8:00 – 12:00 - -
13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 13:00 – 17:00 - -

Supporter timezone: America/New_York (GMT-04:00)

This topic contains 2 replies, has 2 voices.

Last updated by Kelly 3 years, 10 months ago.

Assisted by: Christian Cox.

Author
Posts
#1632319

I'm trying to follow the documentation here: https://toolset.com/documentation/post-relationships/how-to-display-related-posts-with-toolset/displaying-fields-of-grandparents/ to create a sort of breadcrumb trail on a content template.

My hierarchy is like this:
Macrocycle > Mesocycle > Microcycle > Session.

All are One to Many relationships.

I'm in the template for the sessions and want to have a link back to each stage, displayed just like in the example above, with links to each of the posts.

Following the above documentation is giving me only the current page and parent page. The "grandparents" are returning nothing, like this:

[missing] > [missing] > Microcycle title as a link > Session title as a link

This is the view template:
[wpv-post-body view_template="grandparents" item="@macrocycle-mesocycle.parent"] >
[wpv-post-body view_template="grandparents" item="@mesocycle-microcycle.parent"] >
[wpv-post-link item="@microcycle-session.parent"] > [wpv-post-link]

This is the content template is has one Single Field block in, which is selecting Standard Field, post source of Current Post and Post Field of Post Title with a link.

This section of the documentation is really unclear as to where it is talking:

"In the field-insertion dialog, click the Post selection tab, set the A post related to the current post (…) option and select the post type and the relationship in question. In our example, it is “Countries (from Cities Of A Country)”, as shown in the following image."

as there is no "post selection tab" in the block editor for setting up the content template. Is it for an older version and hasn't been updated for the blocks version?

#1632513

Hi, the issue with your breadcrumbs is all about context. I can help you sort this out.

My hierarchy is like this:
Macrocycle > Mesocycle > Microcycle > Session.

Since the following code is working and the wpv-post-link shortcode shows you the link to the Session post, we would say thithis View is in the context of a Session post:

[wpv-post-link item="@microcycle-session.parent"] > [wpv-post-link]

In the context of a Session post, you have exposure to the Session post itself and you can set the context of a shortcode one level up using the @relationship-slug.parent syntax. I like to think of it like holding a picture of a picture of a picture...and so on. I can only switch out the picture in my hand, I can't change any of the pictures inside that picture. So the wpv-post-link shortcode should work okay because it's one level up. However, that's as far as the context allows you to change. From the Session post, you can only change the context up one level of hierarchy. In other words, this code won't work in the context of a Session post, because you cannot change context to mesocycle (2 levels of hierarchy) here:

[wpv-post-body view_template="grandparents" item="@mesocycle-microcycle.parent"] >

The idea is that you'll have to create 2 nested Content Templates to switch context up the hierarchy chain one level at a time. Delete the microcycle link and the two grandparent templates from the View for now. Create a mesocycle-breadcrumb template and a microcycle-breadcrumb template. First, the mesocycle-breadcrumb template will include the macrocycle template in the context of macrocycle from mesocycle, and the post link shortcode to display the mesocycle link. The mesocycle link shortcode needs no context because we will set the template context in the microcycle-breadcrumb template where we place the mesocycle template:

( in the mesocycle-breadcrumb template)
[wpv-post-link item="@macrocycle-mesocycle.parent"] > [wpv-post-link]

Then, the microcycle-breadcrumb template will include the mesocycle template in the context of mesocycle from microcycle, and the post link shortcode - again, it needs no context because we will set the template context in the View where we place the microcycle-breadcrumb template:

( in the microcycle-breadcrumb template)
[wpv-post-body view_template="mesocycle-breadcrumb" item="@mesocycle-microcycle.parent"] > [wpv-post-link]

Then you'll include the microcycle-breadcrumb template in the View, setting the microcycle context from the session context:

(in the Session View)
[wpv-post-body view_template="microcycle-breadcrumb" item="@microcycle-session.parent"] > [wpv-post-link]

Let me know if you run into problems with this.

as there is no "post selection tab" in the block editor for setting up the content template. Is it for an older version and hasn't been updated for the blocks version?
Right, in classic Views you would click the "Fields and Views" button above a text editor to insert one of our shortcodes. A popup would be shown, where you would use the post selection tab to change the context of the shortcode. There's no context changer built-in to the Content Template block at this time.

#1633581

Aaaah that worked. The key thing here was that I was trying to use the block editor which made this impossible. I really couldn't work out how to follow your instructions till I discovered the link to the "classic editor" hidden at the bottom and then it all started making sense.

Resolved now. Thank you!

This ticket is now closed. If you're a WPML client and need related help, please open a new support ticket.