Rah*_*hul 1 wordpress levels categories
让我告诉你情景首先说wordpress中的类别结构是这样的
Level 1: Top
Level 2: -Nextme_1
Level 3: --Nextme_2
--Nextme_3
Level 4: ---Nextme_4
---Nextme_5
Run Code Online (Sandbox Code Playgroud)
现在我需要检查类别的级别是什么?假设我抓到3级的类别,所以我必须使用不同的模板,如果它的级别4.那么我需要使用另一个模板?
有谁可以给我一些提示?
谢谢
Rahul
如果您没有多个类别,可以尝试从管理员编辑他们的slug,然后在您的页面中以这种方式获得类别slug:
if (is_category()) {
$cat = get_query_var('cat');
$category = get_category($cat);
echo 'your slug is '. $category->slug;
}
Run Code Online (Sandbox Code Playgroud)
现在,当你编辑类别时,尝试在它们的级别之后命名它们:cat-lvl-1,cat-lvl-2.然后在你的页面中使用一些php字符串函数从类别slug中提取数字,然后检查该数字:
if ($category->slug == 1 ) {
//load the template for the category of level 1
}
if ($category->slug == 2 ) {
//load the template for the category of level 2
}
Run Code Online (Sandbox Code Playgroud)
等等.
稍后编辑:试试这个:
function get_level($category, $level = 0)
{
if ($category->category_parent == 0) {
return $level;
} else {
$level++;
$category = get_category($category->category_parent);
get_level($category, $level);
}
}
if (is_category()) {
$cat = get_query_var('cat');
$yourcat = get_category($cat);
echo get_level($yourcat);
}
Run Code Online (Sandbox Code Playgroud)