首页 论坛 wordpress不同分类调用不同模板
帖子详情

用wordpress做网站特别是企业网站建设的时候,多个分类的内容不同需要显示的页面也不一样,比如有些分类显示的是产品缩略图,有些显示的是公司新闻公告等,此时需要不同的分类模板。

fl

虽然wordpress默认只有一个通用的分类目录模板(archive.php),但是可以通过函数代码实现不同的分类调用不同的模板。

实现wordpress不同分类调用不同模板的方法

第一步在模板函数里添加获取子分类的函数代码

//获取并输入某个分类的子分类
function post_is_in_descendant_category( $cats, $_post = null )
{
  foreach ( (array) $cats as $cat ) {
  // get_term_children() accepts integer ID only
  $descendants = get_term_children( (int) $cat, 'category');
  if ( $descendants && in_category( $descendants, $_post ) )
  return true;
  }
  return false;
}

//调用子分类、
function get_category_root_id($cat)
{
$this_category = get_category($cat); // 取得当前分类
while($this_category->category_parent) // 若当前分类有上级分类时,循环
{
$this_category = get_category($this_category->category_parent); // 将当前分类设为上级分类(往上爬)
}
return $this_category->term_id; // 返回根分类的id号
}

在当前wordpress主题的category.php文件中(没有就新建一个),添加以下判断代码:

<?php
$post = $wp_query->post;

if (is_category(array("1","2"))){
include(TEMPLATEPATH . '/category-pro.php');
}
elseif ( is_category('3') || post_is_in_descendant_category(3) ){
include(TEMPLATEPATH . '/category-news.php');
}
else {
include(TEMPLATEPATH . '/category-wz.php');
} ?>

该判断函数表示:判断分类id是否为‘1’,2,如果是,则调用category-a.php模板,如果分类id是3则调用category-b.php模板,如果以上两者都不是则调用category-all.php模板.

版权:言论仅代表个人观点,不代表官方立场。转载请注明出处:https://www.52diyhome.com/forum/65132.html

发表评论
暂无评论
  • 0 +

    访问总数

  • 0 +

    会员总数

  • 0 +

    资源总数

  • 0 +

    今日发布

  • 0 +

    本周发布

  • 0 +

    运行天数

资源在于分享,创作来源想象