显示自定义类别和子类别页面的代码是什么?

时间:2016-02-05 作者:Amin Arjmand
hello
We have categories and subcategories like this :
- Category 1
-- SubCategory 1
-- SubCategory 2
-- SubCategory 3
- Category 2
- Category 3
I have one style for all Categories **level 1** and one style for all subcategories **level 2**

I want a code to act like this :

If it was one of Categories level 1 show {
   cat1.php (A page that is fix and show some fix content)
}
else if it was one of SubCategories of level 2 show{
   cat2.php (A page that show only last articles or posts in this subcategory level 2)
}
end;

thanks for your help
1 个回复
最合适的回答,由SO网友:Amin Arjmand 整理而成

我找到了两种解决问题的方法:

1-您可以为所有类别创建一个名为“category.php”的文件,然后为要添加不同样式的每个类别创建一个类似>>“category-id.php”或“category-name.php”的文件。

2-另一种方法是,您可以通过if和else来完成这项工作,您应该使用以下代码:

<?php if (in_category(\'name\')) {
     echo \'htmlcode\';
}
elseif (in_category(\'name-2\')) {
     echo \'htmlcode\';
}
else {
     echo \'htmlcode\';
}
?>

相关推荐