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

 4726

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

  1. //WordPress子分类页面使用父页面模板 
  2. add_filter('category_template', 'f_category_template');
  3. function f_category_template($template){
  4.     $category = get_queried_object();
  5.     if($category->parent !='0'){
  6.         while($category->parent !='0'){
  7.             $category = get_category($category->parent);
  8.         }
  9.     }
  10.  
  11.     $templates = array();
  12.  
  13.     if ( $category ) {
  14.         $templates[] = "category-{$category->slug}.php";
  15.         $templates[] = "category-{$category->term_id}.php";
  16.     }
  17.     $templates[] = 'category.php';
  18.     return locate_template( $templates );
  19. }

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


本文网址:https://www.zztuku.com/index.php/detail-7772.html
站长图库 - WordPress子分类页面使用父分类页面模板
申明:如有侵犯,请 联系我们 删除。

评论(0)条

您还没有登录,请 登录 后发表评论!

提示:请勿发布广告垃圾评论,否则封号处理!!

    编辑推荐