我們在做文章時為了讓網站有更好的用戶體驗都會把相關的不欄目的不分類的使用不同的模式來調用,這樣感覺網站大氣有個性,下面我來給大家介紹wordpress主題不同分類顯示不同樣式模板具體步驟.
下面就是具體實現代碼有需要的童鞋就拿走吧,代碼本人已親測可放心使用,實例代碼如下:
- <?php
- if ( in_category('post') ) {
- include(TEMPLATEPATH . '/single-post.php');
- }
- // elseif 在一次判斷 想在加判斷復制代碼
- elseif ( in_category('plugin') ) { // pro 為category的別名
- include(TEMPLATEPATH . '/single-plugin.php');
- }
- // elseif 結束
- else {
- include(TEMPLATEPATH . '/single-all.php');
- }
- ?>
in_category('post')里面的post還可以支持ID,即寫出 in_category('3').
另一種也差不多我們直接修改樣式
IF IN_CATEGORY 條件標簽
首先,復制兩個single.php文件分別取名為“single1.php” 和“single2.php”,然后,把原先的single.php文件里面的內容全部刪除,并用下面的代碼進行替換:
- <?php
- $post = $wp_query->post;
- if ( in_category(’9′) ) {
- include(TEMPLATEPATH . ‘/single2.php’);
- } else {
- include(TEMPLATEPATH . ‘/single1.php’);
- }
- ?>
意思是:檢查日志,如果日志屬于分類ID9,則顯示single2.php,如果不是,則顯示single1.php.
還可以使用更多的條件語句來為不同的類別指定不同的樣式和內容,例如如下代碼:
- <?php
- $post = $wp_query->post;
- if ( in_category(’9′) ) {
- include(TEMPLATEPATH . ‘/single9.php’);
- elseif ( in_category(’12′) ) {
- include(TEMPLATEPATH . ‘/single12.php’);
- elseif ( in_category(’42′) {
- include(TEMPLATEPATH . ‘/single42.php’);
- } else {
- include(TEMPLATEPATH . ‘/single1.php’);
- } }
- ?>
因為并不想都改變這兩個single模板文件,而僅僅是添加額外的樣式表到第二個也就是single2.php文件,于是再創建兩個header模板文件,就像復制single.php一樣,然后在第二個header文件的頂部添加上外部樣式表的鏈接:
- <style type=“text/css” media=“screen”>
- @import URL(‘/wp-content/themes/mytheme/style.css’);
- @import URL(‘/wp-content/themes/mytheme/cssstyles.css’);
- </style >
接著,就是要在第二個single2.php里面調用header2.php,代碼如下:
- <?php
- /* Don’t remove this line. */
- require(‘./wp-blog-header.php’);
- include(get_template_directory() . ‘/header2.php’
- ?>
新聞熱點
疑難解答
圖片精選