我们开发WordPress主题的过程中,经常遇到需要子分类继续使用父分类的页面模板,这样能大大减少我们的工作量。那么WordPress子分类页面如何使用父页面模板呢?这篇WordPress教程详细为你解答。
- //* WordPress子分类页面使用父页面模板 *//
- add_filter('category_template', 'f_category_template');
- function f_category_template($template){
- $category = get_queried_object();
- if($category->parent !='0'){
- while($category->parent !='0'){
- $category = get_category($category->parent);
- }
- }
- $templates = array();
- if ( $category ) {
- $templates[] = "category-{$category->slug}.php";
- $templates[] = "category-{$category->term_id}.php";
- }
- $templates[] = 'category.php';
- return locate_template( $templates );
- }
使用非常简单,值需要将上面代码复制到主题的functions.php文件中即可。
- 我的微信
- 微信扫一扫
-
- 我的微博
- 微博扫一扫
-
评论