change to custom single.php based on parent category

Hello,
I have a theme I've customized quite a bit using custom templates. My site has 3 parent categories with several child, grandchild and great grandchild categories under them and also what I'll refer to as all other categories. I have been trying to add the below codes to my single.php in my child theme hoping that any post in one of the three parent categories (or any children/grand/greatgrand/etc categories) will make use of the respective custom single.php files I have while all posts in any other categories use the default single.php.

Here are the codes I've tried, one uses a deprecated function "split" and both of them make anything displayed with the single.php (custom ones) template prints 2 times, so after the footer the page is displayed again but does not contain any sidebars.

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 the above and it ends up duplicating the posts like the first attempt, so there is obviously an error. Is this a not in the loop issue?
Here is my second attempt to accomplish this:

<?php
if (has_term( 'emcp-nur', 'category', $post )) :
	get_template_part( 'single', 'emcpn' );

elseif (has_term( 'emcm-nur', 'category', $post )) :
	get_template_part( 'single', 'emcmn' );

elseif (has_term( 'emcmp-tech', 'category', $post )) :
	get_template_part( 'single', 'emcpt' );

else :
	get_header(); 

endif;
?>

But this code is causing the same issue where the posts it applies to is duplicating and displaying twice due to an error in the code.

  • Ivan Shulev

    Hey Ian ,

    If I understood you correctly, please correct me if I haven't, the following code should work for you:

    // custom single template for specific category
    function wpse_custom_category_single_template( $single_template ) {
    
    	global $post;
    
    	// get all categories of current post
    	$categories = get_the_category( $post->ID );
    	$top_categories = array();
    
    	// get top level categories
    	foreach( $categories as $cat ) {
    		if ( $cat->parent != 0 ) {
    			$top_categories[] = $cat->parent;
    		} else {
    			$top_categories[] = $cat->term_id;
    		}
    	}
    
    	// check if specific category exists in array
    	if ( in_array( '99', $top_categories ) ) {
    		if ( file_exists( get_template_directory() . '/99-single.php' ) ) return get_template_directory() . '/99-single.php';
    	}
    	elseif ( in_array( '100', $top_categories ) ) {
    		if ( file_exists( get_template_directory() . '/100-single.php' ) ) return get_template_directory() . '/100-single.php';
    	}
    	elseif ( in_array( '101', $top_categories ) ) {
    		if ( file_exists( get_template_directory() . '/101-single.php' ) ) return get_template_directory() . '/101-single.php';
    	}
    
    	return $single_template;
    
    }
    
    add_filter( 'single_template', 'wpse_custom_category_single_template' );

    It will return the first parent category template.

    I hope this helps and I wish you an awesome day ahead!

    Ivan

Thank NAME, for their help.

Let NAME know exactly why they deserved these points.

Gift a custom amount of points.