I have the following code working but with split() being depreciated and not available in php 7, I need some help. The idea here is this code is inserted in my single.php (in child theme). When a single post is visited the code will check to find the parent category for the post and assign customized single.php file. In this code "Parent ==" is the parent category that specifies the template to use "/single-xxx.php". I need help to rewrite this so it works in php 4-7, wordpress 4.0+:
<php?
function get_top_category() {
$category = get_the_category();
$cat_tree = get_category_parents($category[0]->term_id, FALSE, ':', TRUE);
$top_cat = split(':',$cat_tree);
return $parent = $top_cat[0];
}
//echo '<pre>';print_r(get_top_category());echo '</pre>';
$Parent = strtolower(get_top_category());
if($Parent == "emcp-nur") {
include(get_stylesheet_directory() . '/single-emcpn.php');
}
else if($Parent == "emcm-nur") {
include(get_stylesheet_directory() . '/single-emcmn.php');
}
else if($Parent == "emcp-tec") {
include(get_stylesheet_directory() . '/single-emcpt.php');
}
get_header(); ?>
Along the same lines I am trying to use the following in place of this and I must be putting it in the incorrect place. Much like the above code which I placed above the get_header() the posts where the script applies end up duplicating one page and then the next, so there is obviously an error. Here is another attempt to accomplish the same thing:
if (has_term('emcp-nur', 'category', $post)) get_template_part('single', 'emcpn');
else if(has_term('emcm-nur', 'category', $post)) get_template_part('single', 'emcmn');
else if(has_term('emcp-tech', 'category', $post)) get_template_part('single', 'emcpt');
else get_header(); ?>
But this code is causing the same issue where the posts it applies to are duplicating the display due to an error in the code.
explode
instead ofsplit
.