[Resolved] How to output a hierarchical post type respecting its hierarchy
This thread is resolved. Here is a description of the problem and solution.
Problem:
A post type is hierarchical, meaning the posts can have child posts, which can have child posts etc.
These should be output in a list with each parent showing its child posts indented etc.
Solution:
You need nested Views, one for each level of the hierarchy.
The first View displays only the parents by including a Query Filter to only return posts without a parent post (i.e. the top level posts).
In the output section, between the wpv-loop tags, a second View can be inserted to display the child posts, which would be achieved with a Query Filter "Parent is the current post in the loop".
I am using your plugin for a website and it's working great. I am having a challenge with one of my custom post types. I would like to show them in the front end as nested elements. Ex:
1.1 Dummy Text
1.1.1 Child Element
1.1.1.1 Child Child Element
All these items belong to the same custom post type, and I am creating the relationship using the built-in Page Attribute feature in WordPress to assign Parent Pages. (see screenshot).
At the moment I am using a View to display the custom post types items in my front end. Code Below:
So it's basically looping through the element and listing them. But I would need some kind of conditional, to identify if the post had a parent page so we can nested inside and add a class. I am thinking something like this:
[wpv-layout-start]
[wpv-items-found]
<!-- wpv-loop-start -->
<wpv-loop wrap="1" pad="true">
[wpv-item index=1]
"if post has not parent page show"
<div class="individual-study" class="first-level">
<div class="col-sm-12">
[wpv-post-body view_template="Loop item in Individual Studies View"]
</div>
</div>
"end if" else "if post has 1 parent page show"
<div class="individual-study" class="second-level">
<div class="col-sm-12">
[wpv-post-body view_template="Loop item in Individual Studies View"]
</div>
</div>
"enf if" else "if post has 2 parent page show"
<div class="individual-study" class="third-level">
<div class="col-sm-12">
[wpv-post-body view_template="Loop item in Individual Studies View"]
</div>
</div>
</wpv-loop>
<!-- wpv-loop-end -->
[/wpv-items-found]
[wpv-no-items-found] [wpml-string context="wpv-views"]No items found[/wpml-string]
[/wpv-no-items-found]
[wpv-layout-end]
Is that possible, also that they are nested, so the second level inside the first level and third level inside the second level?
What you are describing wouldn't really work because you wouldn't necessarily get the posts all output in just the right order.
You need nested Views.
The first View will query the post type and include a Query Filter to only return posts without a parent post (i.e. the top level posts), as shown in the screenshot.
The Loop Output section might look a little like this:
See that I'm including a nest View. This View would essentially be the same, but the Query Filter this time would be the "Parent is the current post in the loop", and the loop output might indent the title and would include a third View.
You could go on like this for as many levels as you have in the hierarchy.
You will likely want to delete everything within the wpv-no-items-found shortcodes to avoid outputting anything where there are no descendent posts.