2011-04-06 32 views
0

我正在基於用戶選擇的類別爲Wordpress中的single.php創建多個佈局。我已經看到了這兩個類別,並在過去這樣做。但是我還沒有嘗試使用更多的兩個自定義single.php文件。它看起來很直截了當。我在我的single.php文件中創建了一些if語句,將用戶重定向到適當的模板。但是,我只是得到一個空白頁面。這是我在single.php文件中的代碼。基於類別ID的Wordpress中的多個single.php

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

if (in_category('12')) { 
include(TEMPLATEPATH . '/single12.php'); 
} 

elseif (in_category('3')) { 
include(TEMPLATEPATH . '/single3.php'); 
} 

elseif (in_category('1') { 
include(TEMPLATEPATH . '/single1.php'); 
} 

else { 
include(TEMPLATEPATH . '/single-default.php'); 
} 

?> 

回答

-1

您是否聲明瞭全局wp_query?你可能有錯誤報告關閉,因此這就是爲什麼你seing一個空白頁,所以嘗試:

global $wp_query; 
$post = ... 

堅韌作爲一個更好的做法,你可以使用下面的代碼(不是測試,但它應該解釋一下我的意思是更好實踐):

global $wp_query; 
$post = $wp_query->post; 
$categoryes = get_the_category($post->ID); 
if (count($categoryes) > 0) 
{ 
    $disalowedCategories = array(4,6,8); // these categories should use single-default.php 
    $category = $categoryes[0]; 
    $templateFile = TEMPLATEPATH . '/single' . $category->cat_ID . '.php'; 

    if (file_exists($templateFile) && !in_array($category->cat_ID, $disalowedCategories)) 
    { 
     include($templateFile); 
    } else { 
     include(TEMPLATEPATH . '/single-default.php'); 
    } 
}