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.
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:
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.
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!
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