samedi 28 février 2015

How to give all CPT a folder automatically based on their slug


This is what I have now and working:



function get_custom_post_type_template($single_template) {
global $post;

if ($post->post_type == 'amazon') {
$single_template = TEMPLATEPATH . '/amazon/single.php';
}
return $single_template;
}
add_filter( 'single_template', 'get_custom_post_type_template' );


But I want something like this:



add_filter('single_template', 'my_single_template_folders_terms');
function my_single_template_folders_terms($template) {
global $wp_query;
foreach( get_post_type(array('public' => true, '_builtin' => false)) as $post_type ) {
if ( file_exists(TEMPLATEPATH . "/{$post_type->slug}/single.php") )
return TEMPLATEPATH . "/{$post_type->slug}/single.php";
}
return $template;
}


I got this error; Warning: Invalid argument supplied for foreach()





Aucun commentaire:

Enregistrer un commentaire