WordPress技巧:为不同的页面使用不同的头部文件


在wordpress中如何为不同的分类定义不同的头部文件
你是不经常遇到这种情形,你希望对不同的分类运用不同的头部样式?我确定你肯定有这种需求。这里有一些简单有效的解决方案让你能够维布通的主题定义不同的头部样式。如果你需要更细节的内容,你可以去查看wordpress的开发文档。
首先你要为不同的分类创建不同的头部文件,需要有一个头部文件是默认的。这些文件将会被包含于主要的header.php 文件。下面的代码就是wordpress 主题模板需要的代码。
<?php
/**** Conditional Header for Per Category Example Wordpress ****/
//  make sure we are in category listing
if (is_category()){
if (is_category(’1′)){
<?php include(TEMPLATEPATH.’/header-cat1.php’); ?>
}
elseif (is_category(’2′)){
<?php include(TEMPLATEPATH.’/header-cat2.php’); ?>
}
else {
// this is the deafult header
<?php include(TEMPLATEPATH.’/headerdefault.php’); ?>
}
}
?>
下面是用分类的名字和别名的例子
<?php
//  Alternative for using Category ID, You can use Category Name or Slug
if (is_category()){
// example of using a category name
if (is_category(’Category 1′)){
<?php include(TEMPLATEPATH.’/header-cat1.php’); ?>
}
// example of using category slug
elseif (is_category(’category-1′)){
<?php include(TEMPLATEPATH.’/header-cat2.php’); ?>
}
else {
// this is the deafult header
<?php include(TEMPLATEPATH.’/headerdefault.php’); ?>
}
}
?>
可能你也想为不同的页面使用不同的头部文件,所以我也把下面的代码例子写了出来。
<?php
//
// example of using header file based on pages
// note that you can either use page ID, Page Name or Page Slug
//
// this one uses page title
if (is_page(’About’)){
<?php include(TEMPLATEPATH.’/header-contact.php’); ?>
}
// this one uses page slug
elseif (is_page(’subscribe’)){
<?php include(TEMPLATEPATH.’/header-subscribe.php’); ?>
}
// this one uses page ID
elseif (is_page(’6′)){
<?php include(TEMPLATEPATH.’/header-contact.php’); ?>
}
else {
<?php include(TEMPLATEPATH.’/headerdefault.php’); ?>
}
?>
提示:你需要根据你的实际情况用上面这些代码写入你的header.php文件来加载特殊的头部元素。

« 
» 
快速导航

Copyright © 2016 phpStudy | 豫ICP备2021030365号-3