我正在设计一个新模板来替换现有模板。该模板名为featured.php,我将用featured-new.php替换它在WordPress中使用PHP手动设置模板
我想在查询字符串中传递变量“new”,那么如果存在new
变量,它将使用featured-new.php文件。
我该怎么做?是否有标签告诉页面使用指定的模板?
我正在设计一个新模板来替换现有模板。该模板名为featured.php,我将用featured-new.php替换它在WordPress中使用PHP手动设置模板
我想在查询字符串中传递变量“new”,那么如果存在new
变量,它将使用featured-new.php文件。
我该怎么做?是否有标签告诉页面使用指定的模板?
我会使用theme switcher
插件,例如这个one,然后自定义是否只显示URL或打开布局。
而且,看看这个:Theme Switching
我认为,要做到这一点最简单的方法是的if/else当前themplate,包括新的templete或featured.php
不
if($_GET['show'] == 'new')
{
include('featured-new.php');
}
else
{
//this is the full featured.php code
}
我假定这是一个页面模板更多的帮助,是这样吗?这意味着它在顶部有一个评论“模板名称:”,当页面出现时使用该评论,并且该名称已从编辑器的“页面模板”菜单中选择。如果多数民众赞成的情况下,然后在同一页...(双关语是不可避免的)。
如果是这样的话,你需要过滤'page_template'。
function filter_page_template($template){
/**
* Lets see if the current template is featured.php,
* and if 'new' is set in the query string
*/
if(isset($_GET['new']) && $template == locate_template('featured.php')) {
/**
* If so, we'll set the custom template name...
* You can skip this temporary variable and just pass this
* directly to 'locate_template()'.
*/
$new_template = 'featured-new.php';
/*... and now we use locate_template to find the actual path and return that. */
return locate_template($new_template);
} else {
/**
* Otherwise, if 'new' is not set
* or the current template is not 'featured.php',
* We should just return the original $template value undisturbed.
*/
return $template;
}
}
add_filter('page_template', 'filter_page_template');
我不明白你怎么想用“新”,听起来像要想要一种被称为新的查询字符串参数,看看它只是设置的。这就是我所做的。如果你的意思不同,你需要相应地改变这一点。