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' );