条件满足时是否加载不同的模板文件?

时间:2012-06-19 作者:Scott

如果我访问WordPress网站上的一个页面,WP会根据页面的大小决定应该使用什么样的模板文件template hierarchy.

如果我在钩子里做一些逻辑检查init 如果我的条件得到满足,我如何告诉WordPress加载我选择的模板文件,而不是WordPress通常会提供的模板文件?

2 个回复
最合适的回答,由SO网友:Stephen Harris 整理而成

您能在template_include 滤器否则,您可以设置全局/常量并在template_include 提供适当的模板。

template\\u include筛选器文件管理要包含的模板文件的路径。为了避免错误,您应该检查模板是否存在-locate\\u template对主题/子主题文件执行此操作。

add_filter("template_include", "sc_redirect_properties_to_registration");
function sc_redirect_properties_to_registration( $template )
{
    if( is_user_logged_in() && ! is_page("the-properties") )
        return $template;

    //You could include templates from plugins
    //$template = plugin_dir_path(__FILE__).\'templates/plugin-template.php\';

    //Best to check the template exists. With **theme/child-theme** templates
    // you can do this with locate_template.
    return locate_template(\'template-registration.php\');
}

SO网友:Michael Ecklund

我想您可以使用如下条件:

if(is_front_page()){
    // Home page layout
} elseif(is_page()){
    // General page layout
    if(is_page(\'contact\')){
        // Page layout specific to the contact page
    } elseif(is_page(\'about\')){
        // Page layout specific to the about page
    }
}
然后根据网站的不同部分,使用get_template_part();

结束

相关推荐

Run shortcode before filters

我的用户在注释中发布代码片段。我为此创建了一个快捷码:function post_codigo($atts,$content=\"\"){ return \'<code>\'.$content.\'</code>\'; } add_shortcode(\'codigo\',\'post_codigo\'); 问题是html在打包到代码标记之前会被过滤掉。我想如果我能在过滤器之前运行短代码,那么我可以使用fun