After taking a look through the questions suggested to possibly have my answer, I still can not seem to find something that I references the post-template php files.
I have two custom post types that I am attempting to combine into a single post-type. I will call the custom post types 'Client' and 'Project'. What I have done is create a 'Clients & Projects' custom post type, and set each one of the 'Clients' to parent and the subsequent 'Projects' to Children within the 'Clients & Projects'.
I have PHP templates created already, and they work fine for each of the post types individually, but I can't figure out how to set one of the templates to load for the 'Clients' (parent-posts) and the other template to load for the 'Projects' (child-posts).
The naming of the files is as follows:
- Clients would be clients_projects_parent.php
- Projects would be clients_projects_child.php
I found this code snippet here, but it did not solve my problem.
function switch_page_template() {
global $post;
// Checks if current post type is a page, rather than a post
if (is_page()){
// Checks if page is parent, if yes, return
if ($post->post_parent == 0)
return true;
else if ($post->post_parent != $post->ID){
$parent_page_template = get_post_meta($post->post_parent,'_wp_page_template',true);
$template = TEMPLATEPATH . "/{$parent_page_template}";
if (file_exists($template)) {
load_template($template);
exit;
}
}
}
}
add_action('template_redirect','switch_page_template');
My ultimate hope would be for each of the Parent and Child posts to load a different PHP template respectively.
Update:
I was tinkering around and tried this:
add_action('template_redirect', 'get_template');
function get_template(){
global $wp;
global $wp_query;
if(is_page('client_project') || '0' === $post->post_parent){
include(TEMPLATEPATH . '/single-client_project.php');
}elseif(is_page('client_project') || $post->post_parent > 0){
include(TEMPLATEPATH . '/single-client_project_child.php');
}
else{
include(TEMPLATEPATH . '/single-client_project.php');
}
}
I placed a conditional statement in the templates that I am trying to call/redirect as well that looks like this:
global $post;
if ( is_page('client_project') || '0' == $post->post_parent ) {
echo "<h1>This is a parent page</h1>";
echo "<p>post parent = " . $post->post_parent . "</p>";
} else {
echo "<h1>This is a child page</h1>";
echo "<p>post parent = " . $post->post_parent . "</p>";
}
Now, what is happening is when the posts load each one gives the correct indication from the echo loop that was placed in the template files ie. parent post prints 'This is a parent page' and gives the correct post parent number '0', but the templates are still loading wrong (ether the parent or the child template loads not each one for each type)...
I only tried this after seemingly every other method failed me. I am sure that I am missing something, but I really can't figure it out.
FINAL UPDATE:
I finally got the templates to swap out. Although it is in what I can only imagine is the worst possible way. I basically went into the 'parent' template and placed an if statement just before the code of the page. It looks like this:
<?php
global $post;
if(is_page('client_project') || $post->post_parent > 0){
include(TEMPLATEPATH . '/single-client_project-child.php');
}
?>
I read after I got this to work that TEMPLATEPATH was depreciated, so I will most likely swap that out, but I am going to call this answered, @G. M. if you wanted to, I would really appreciate you writing the correct way to do this, or at the very least a better way. I invite everyone who comes across this question to do the same. Cheeers!