Skip Navigation

[Resolved] Displaying hierarchical content

This support ticket is created 6 years, 11 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
- 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 7:00 – 14:00 -
- 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 15:00 – 16:00 -

Supporter timezone: Europe/London (GMT+00:00)

This topic contains 5 replies, has 2 voices.

Last updated by Armando 6 years, 11 months ago.

Assisted by: Nigel.

Author
Posts
#604920

Tell us what you are trying to do?

I have one CPT. It has hierarchical display. For example:

Post 1
Post 2
Post 3
-Post 4
-Post 5
-Post 6
Post 7
Post 8
-Post 9
-Post 10

I only have one CPT, so the Views Parent/Child doesn't apply.

I would like to:

Display all parent CPT (title with link). This I can do.
Display all children of CPT (title with link). This I can do.

What I need help with:
Show parent, and if it has children, display link to page that shows only children.

Post 1
Post 2
Post 3
Post 7
Post 8

If I click on Post 3, I go to page showing (titled with link):
Post 4
Post 5
Post 6

I know this has been covered before, but I cant seem to find docs/forum posts, since I am only using one CPT and using WP hierarchical for my parent/children.

Thank you!!!

#605043

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

parent-filter.png

Hi Armando

You would typically use URL parameters to pass the parent ID to the View which displays the child posts for this.

So, you have a View which only shows the top level custom posts (the parents).

In the Loop Output section you construct a link to a page where you will have a second View that displays the second level posts (the child posts) and pass the parent post ID as a URL parameter, like so:

		<wpv-loop>
          <h3>[wpv-post-link]</h3>
          <a href='[wpv-post-url id="177"]?parent=[wpv-post-id]'>Child posts</a>
		</wpv-loop>

Then your second View will include a post parent filter as shown in the screenshot parent-filter.png.

The only problem here is that the link to the child posts will appear for all parent posts, even those that don't have any (and so clicking the link will take the visitor to a page that shows a No results found message.

What you could do is to have two versions of the View that shows the child posts, one as described already.

The second one would change the parent post filter so that the parent was set by the current post in the loop.

You would move the link that you currently generate in the parent post View into this child post View (you would need to add the id="$parent" attribute to the wpv-post-id shortcode to fetch the id of the parent), and in the no items found section you would output nothing at all.

You then edit the parent post View and insert this second child post View in place of the link creation.

So the link would only be generated if there were child posts to show.

That sounds a little complex but if you try implementing it you will hopefully follow what I'm describing.

#606041

Thank you for the thorough reply Nigel. I am working on Implementing your solution. I ran into a snag but I will try something else before I return for further assistance. Please bare with me a few days. Thank you!

#606258

Nigel
Supporter

Languages: English (English ) Spanish (Español )

Timezone: Europe/London (GMT+00:00)

No problem, let me know how you get on.

#608062

Hi Nigel. I have implemented your solution, but Need a few adjustments (or I didnt do it correctly). What I have so far:

Parent View
Filter:
Top Level Posts with no parent
Loop:

<wpv-loop>
          <ul>
            <li>
          <h3>[wpv-post-link]</h3>
          <div>[wpv-post-featured-image]</div>
          [wpv-view name="child-portfolios-duplicate"]
            </li>
          </ul>
		</wpv-loop>

Child View
Filter:
Select posts that are children of the Post with ID set by the URL parameter wpv-child-of.
eg. hidden link
Loop:

<wpv-loop>
			<li>
              [wpv-post-featured-image]
              [wpv-post-link]
			</li>
		</wpv-loop>

Child View Duplicate
Filter:
Select posts whose parent is the current post in the loop.
Loop:

<wpv-loop>
			<li>
              [wpv-post-featured-image]
              <a href='[wpv-post-url id="1013"]?parent=[wpv-post-id id="$parent"]'>Child posts</a>
			</li>
		</wpv-loop>

In your instructions, you didn't mention what to do with there original Child View, so I don't think its used.

My results:
Parent
Parent
Parent
-Child
-Child
Parent
Parent
-Child
Parent
Parent

The children posts are showing on parent view and clicking child link takes me to page showing all results.

Any advice would be appreciated.

Would this be easier accomplished with a taxonomy?

Thank you for your time, Nigel.

#608145

Ok, with your help I managed to get a solution:
I created a custom shortcode which checks to see if post has children:

//Adds filter to register shortcode
add_filter('wpv_custom_inner_shortcodes', 'prefix_add_my_shortcodes');

function prefix_add_my_shortcodes($shortcodes) {
    $shortcodes[] = 'childpages';
    return $shortcodes;
}

// Shortcode to check if page has children
function list_child_pages() {
	global $post;
        //my-portfolio is my custom post type slug
	 $children = get_posts( array( 'post_type' => 'my-portfolio', 'post_parent' => $post->ID ) );
	 return count($children);
	}
add_shortcode('childpages', 'list_child_pages');

In my parent view, I have:

<wpv-loop>
          <ul>
            <li>
//1013 is the ID of the page where my child view is in
[wpv-conditional if="( '[childpages]' gt '1' )"]<a href='[wpv-post-url id="1013"]?wpv-child-of=[wpv-post-id]'>[wpv-post-title]</a>[/wpv-conditional]
             
[wpv-conditional if="( '[childpages]' eq '0' )"] <h3>[wpv-post-link]</h3>[/wpv-conditional]
             
              <div>[wpv-post-featured-image]</div>
            </li>
          </ul>
</wpv-loop>

In my page (ID 1013 in my case), I have the view:
Filter:
Select posts that are children of the Post with ID set by the URL parameter wpv-child-of.
eg. hidden link

<wpv-loop>
			<li>
              [wpv-post-featured-image]
              [wpv-post-link]
			</li>
		</wpv-loop>

Works!

Thank you for your guidance.