0
votes

can any one help me or translate what I want in php: I need php code like that: If child categories posts from the parent category number (1) {

will include templatepath'/single-Arabic.php

} else if child categories posts from the parent category number (2) {

will include templatepath'/single-English.php

} best regards

2

2 Answers

0
votes

It's better to check for category name rather than category ID. So lets say you have two categories, "fruit" and "meat", then in your single.php just after the get_header(); call, add these lines:

if(has_term('fruit', 'category', $post)) get_template_part('single', 'fruit');
else if(has_term('meat', 'category', $post)) get_template_part('single', 'meat');
// else...

This will work if you've created the files single-fruit.php and single-meat.php

More here: https://codex.wordpress.org/Function_Reference/get_template_part

0
votes

here is the function to get top parent category.

let suppose you have category

1 . English
-- sub 1 English category
-- sub 2 English category

2 . Arabic
-- sub 1 Arabic category
-- sub 2 Arabic category

and you are on the post page where it say current category sub 2 English then you call function

$Parent = strtolower(get_top_category());

it will return top category name which is English, after that condition will check if category is English then include 'single-English.php'

vice versa for Arabic.

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 == "english") {
    include 'templatepath/single-English.php';
}
else if($Parent == "arabic") {
    include 'templatepath/single-Arabic.php';
}