2012-03-08 93 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']值做一些验证,但是你也许想要保留一个运行列表来检查,以便它们不能简单地使用任何旧模板。