1
votes

I have created a hierarchical custom post type in wordpress called "films." It is hierarchical so it can have children elements.

When a "film" is clicked, wordpress automatically uses the template called 'single-films.php'. That is great, but I wish to use a different template when one of the film's children pages is clicked. For instance, a child of a film might be "press." When that film's press link is clicked, I want it to use a different template than single-films.php.

I was hoping there is some way I can use a template like single-children-films.php. Any ideas how I can change a hierarchical custom post type's children template?

2
or, another way of handling it is to add conditional logic to the single-films.php saying if this post is a parent, do this...if this post is a child, do this....But I'm unsure of the code to do that either.JCHASE11
I just answered something very similar here: stackoverflow.com/questions/45919508/…Paul

2 Answers

2
votes

I think I have just done the same thing:

I had a custom post type called "product" the parent is the 'brand' and the child is essentially an 'item of the brand' here is what I put in my single-product.php

This is the first query to get the child posts [items]

 <?php $mypages = get_pages( array( 'child_of' => $post->ID, 'post_type' => 'product', 'sort_order' => 'desc' ) );
//So if it is not a parent
if (empty($mypages)){ ?>
//use these styles
<div class="single_item">stuff is happening</div>
//If it is a parent post so has children
 <?php ; } else { ?>
//Then Do these things
<div class="brandstuff">showing extra stuffs</div>
<div class="morebrandstuffs">maybe do some extra stuff</div>
//I wanted to add a list of my child pages to my parent page
<?php $mypages = get_pages( array( 'child_of' => $post->ID, 'post_type' => 'product', 'sort_order' => 'desc' ) );

foreach( $mypages as $page ) {      
    $content = $page->post_content;
    if ( ! $content ) // Check for empty page
        continue;

    $content = apply_filters( 'the_content', $content );
?><div class="item">
    <a href="<?php echo get_permalink( $page->ID ); ?>">
    <div class="item_thumb"><?php echo get_the_post_thumbnail($page->ID, 'item_thumb'); ?></div>
    <?php echo $page->post_title; ?></a>
      </div>

<?php
}   
?> <?php } ?>
0
votes

I'm certain I can help out with this. I just need to know one thing, are the children always of the same post type as the parent? Or is "press" as you said in your example its own separate custom post type?