7.18.2010

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文件來加載特殊的頭部元素。

0 留言:

發佈留言

您使用留言則表示同意及遵守使用條款及守則

建議: 為方便留言回覆,請不要用匿名方式 留言。