2012-03-08 202 views
2

是否有可能有一個網頁,如:www.site.com/page/顯示頁面內容 - WordPress的

,並顯示不同版本的模板使用,說:

WWW。 site.com/page/?template=default

www.site.com/page/?template=archive

...?

因此,它檢索相同的頁面內容,但以不同的方式顯示它。

這是可能與WordPress?它是標準的還是需要一些tomhackery來做到這一點?

謝謝

+1

你可以看看這個,看看它是否有助於解決問題,或幫助你擴大這個問題? [使用具有相同頁面內容的不同頁面模板](http://wordpress.org/support/topic/using-different-page-templates-with-the-same-page-content) – 2012-03-08 16:22:23

回答

2

創建一個'主'模板並將其分配給您的頁面。主模板不包含任何佈局信息 - 只是一組基於GET變量選擇「真實」模板的條件包含語句。主模板可能看起來像這樣:

<?php 
switch ($_GET["template"]) { 
    case "foo": 
     include(TEMPLATEPATH . "/foo.php"); 
     break; 
    case "bar": 
     include(TEMPLATEPATH . "/bar.php"); 
     break; 
    case "baz": 
     include(TEMPLATEPATH . "/baz.php"); 
     break; 
    default: 
     include(TEMPLATEPATH . "/default_template.php"); 
     break; 
} 
?> 
+0

嗨Gabe。是的,這也是我的想法。我之前就已經這樣做了,但以防萬一我這樣做有點傻。感謝您的保證:) – michaelmcgurk 2012-03-08 16:25:01

+0

感謝您的更新,Gabe。請看看:) – michaelmcgurk 2012-03-08 16:37:37

+0

感謝您接受這一點 - 我刪除了我最初的錯誤答案,並更好地說明了正確的版本。 – 2012-03-10 15:24:20

3

剛纔我回答了一個類似的問題。

Manually set template using PHP in WordPress

上面的答案應該工作,但使用TEMPLATEPATH的,我認爲是不理想的,它似乎也沒有好好利用一下WordPress是已經在做選擇的模板。

function filter_page_template($template){ 

     /* Lets see if 'template is set' */ 
     if(isset($_GET['template'])) { 

      /* If so, lets try to find the custom template passed as in the query string. */ 
      $custom_template = locate_template($_GET['template'] . '.php'); 

      /* If the custom template was not found, keep the original template. */ 
      $template = (!empty($custom_template)) ? $custom_template : $template; 
     } 

     return $template; 
} 
add_filter('page_template', 'filter_page_template'); 

這樣做,您不需要爲每個您想要指定的模板添加一個新行。此外,您還可以利用現有的模板層次結構,並考慮輸入不存在的模板的可能性。

我會指出你應該在使用它之前對$ _GET ['template']值做一些驗證,但是你也許想要保留一個運行列表來檢查,以便它們不能簡單地使用任何舊模板。