WordPress教程

WordPress子分类页面使用父分类页面模板

阿里云

我们开发 WordPress 主题的过程中,经常遇到需要子分类继续使用父分类的页面模板,这样能大大减少我们的工作量。那么 WordPress 子分类页面如何使用父页面模板呢?这篇 WordPress 教程详细为你解答。

  1. //WordPress子分类页面使用父页面模板
  2.  
  3. add_filter('category_template', 'f_category_template');
  4. function f_category_template($template){
  5. 	$category = get_queried_object();
  6. 	if($category->parent !='0'){
  7. 		while($category->parent !='0'){
  8. 			$category = get_category($category->parent);
  9. 		}
  10. 	}
  11.  
  12. 	$templates = array();
  13.  
  14. 	if ( $category ) {
  15. 		$templates[] = "category-{$category->slug}.php";
  16. 		$templates[] = "category-{$category->term_id}.php";
  17. 	}
  18. 	$templates[] = 'category.php';
  19. 	return locate_template( $templates );
  20. }
也想出现在这里?联系我们
创客主机

使用非常简单,值需要将上面代码复制到主题的 functions.php 页面即可。

WordPress 子分类页面使用父分类页面模板

已有 266 人购买
查看演示升级 VIP立刻购买

收藏
(0)

发表回复

热销模板

Ashade - 作品展示摄影相册WordPress汉化主题
LensNews

本站承接 WordPress / PbootCMS / DedeCMS 等
系统建站、仿站、开发、定制等业务!