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

王朝学院·作者佚名  2016-08-09
窄屏简体版  字體: |||超大  

在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文件来加载特殊的头部元素。

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航