I have a custom post type named 'project' and using a template for it "single-project.php" which is working fine.
Now I want to have a some differentiation, what I need to assign the template "single-project.php" if a custom value in 'project' post type is mobile and "project-website.php" template if that custom meta value is equal to website.
means, need code to change the templates dynamically on the basis of custom meta value.
Thanks
CodePudding user response:
The easiest way is to just call the "project-website.php" file from the "single-project.php" file, if the custom meta value is equal to "website".
Edit your "single-project.php" file:
<?php
$custom_field_value = get_post_meta($post_id,'your-custom-meta-key',true);
if($custom_field_value == 'website'){
echo get_template_part('single-project');
} else {
*** ENTER YOUR CURRENT CODE FOR SINGLE-PROJECT.PHP HERE ***
}
?>
CodePudding user response:
@Jeremy good point, I already know this but I did it this way , Thanks,
function get_custom_post_type_template($single_template)
{
global $post;
$object = get_queried_object();
$id = $object->ID;
$post_type = $object->post_type;
$is_mobile = get_field('is_mobile_app', $id);
if ($is_mobile !== true && $post_type == 'project') {
return $single_template = dirname(__FILE__) . '/single-project-website.php';
} else {
return $single_template;
}
// return $single_template;
}```