different template for first and second level custom post page

I have a custom post type, for examle, products. They can be multilevel, like pages, f.e.,

Products (custom post type)

    Books
    |- LOTR
    |- Sherlock Holmes
    |- Cooking help
    |- etc

    CD
    |- Iron maiden
    |- AC/DC
    |- Brainstorm
    |- etc

    Manuals
    |- MS Word manual
    |- MS Excel manual
    |- PHP manual

I need a seperate pages for first level of hierachy (Books, CD, manuals), what i can achieve by creating a file archive-products.php in theme directory and then making a new query with parameter to get only first level.

The problem is with the second level. I cannot figure out, how to create a template file for displaying a child elements of f.e. Books ?

Of course, i can manage it by using jquery. Get all level of hierarchy, then hide second level and on the click of first level Name, open sublevel and hide first level, but this is not a solution what i need to achieve, because of the lack of the correct url. I want to get correct url for 1 level www.ddd.com/products/ and also for the second www.ddd.com/products/books/

How would you accomplish this?

UPDATED

This is what I have in my archive-POSTTYPE.php file.

<?php get_header();?>
            <div id="maincol">
            <?

    $query = new WP_Query( 'post_type=produkts&post_parent=0' );
    while( $query->have_posts() ) : $query->the_post();
    echo '<li>';
    the_title();
    echo '</li>';
    endwhile;
    wp_reset_postdata();    

            ?>

                </div>

<?php get_footer();?>

and the single-posttype.php

<?php get_header();?>
        <div id="maincol">
            <?php

            if (is_child_post()){
                //this is the true single product
                the_title();

            }else{
                //this is the product list of parent 

                $query = new WP_Query( 'post_type=produkts&post_parent=".$post->ID."' );
                while( $query->have_posts() ) : $query->the_post();
                echo '<li>';
                the_title();
                echo '</li>';
                endwhile;
                wp_reset_postdata();
            }       

            ?>
            </div>
<?php get_footer();?>

1 Answer
1

i needed something similar and i’ve come up with this simple little function that make things easier :

function is_child_post($parent_ID = null) {
    global $post;
    if (isset($parent_ID) && $parent_ID != null){
        return $post->post_parent == $parent_ID;
    }
    return $post->post_parent > 0;
}

Usage:

check if a post/page/custom has a parent:

if (is_child_post()){
  //yes we have a parent
}else{
  //not its a top level parent
}

check if a post/page/custom is a child of a specific parent with the ID of 32:

if (is_child_post(32)){
  //yes its a child of a parent with the ID of 32
}else{
  //not its a child of a parent with the ID of 32
}

So once you have this handy function you can create your a child-products.php template file and then use ‘template_redirect’ hook to tell WordPress when to use it for example this will tell WordPress to use it even time its a child product:

function custom_child_template_redirect($single_template) {
    global $post;
    if ($post->post_type == 'YOUR_TYPE' && is_child_post()) { //change YOUR_TYPE to your custom Post type name 
        return TEMPLATEPATH . '/child-products.php'; // change child-products.php to the name of your child template file
    }
    return $single_template;

}
add_action( 'template_redirect', 'custom_child_template_redirect' );

Leave a Comment