0

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.

2
  • What is the exact output ? and use explode instead of split.
    – Sumit
    Commented May 13, 2016 at 5:55
  • Which output? The post header and body show up again under the full post. No idea how to write the code for explode with all the considerations needed for belonging to multiple categories, multi level categories and fallback.
    – rezon8dev
    Commented May 14, 2016 at 1:10

1 Answer 1

0

I don't know what our single-XXX.php files look like, but if they include calls to get_header (and perhaps get_sidebar()and get_footer) you obviously will get the header twice as it is called again at the bottom of your first code block (and perhaps te rest of the regular page).

In the second code block you avoid outputting the header twice, because it is in the final else statement, but if there is more code below it will still be executed after the call to single-XXX.php. You should make sure the whole code of the regular single page is inside the else statement.

Otherwise, you could make a template called single-normal, place the full code there and replace the line else get_header(); in your last code block with else get_template_part('single', 'normal');.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.