模板重定向模板已加载,但标题404

时间:2013-06-14 作者:Iamzozo

我已经为给定url创建了一个模板重定向,例如:test。com/测试

测试是页面不存在,我只是检查query\\u vars中的url,如果匹配,我加载模板

include(file.php);
exit;
The page loads and show what i expect, but the title is Page not found. 我用一个过滤器解决了这个问题,但我看到整个页面都有404状态。顺便说一句,我看到内置的多站点激活消息也有一个404头。(我正在使用localhost。)

如果不在wp中创建页面本身,我如何解决这个问题?

1 个回复
最合适的回答,由SO网友:Adam 整理而成

您实际上是在尝试创建一个“假”页面,而不必在WordPress数据库中创建物理页面,为此,您需要自定义重写规则。

有关更多详细信息,请参见我的回答:Setting a custom sub-path for blog without using pages?

快速概述:

步骤1:设置自定义重写规则

add_action(\'init\', \'fake_page_rewrite\');

function fake_page_rewrite(){

    global $wp_rewrite;
    //set up our query variable %test% which equates to index.php?test= 
    add_rewrite_tag( \'%test%\', \'([^&]+)\'); 
    //add rewrite rule that matches /test
    add_rewrite_rule(\'^test/?\',\'index.php?test=test\',\'top\');
    //add endpoint, in this case \'test\' to satisfy our rewrite rule /test
    add_rewrite_endpoint( \'test\', EP_PERMALINK | EP_PAGES );
    //flush rules to get this to work properly (do this once, then comment out)
    $wp_rewrite->flush_rules();

}
步骤2:在匹配查询变量时正确地包含模板文件
add_action(\'template_redirect\', \'fake_page_redirect\');

function fake_page_redirect(){

    global $wp;

    //retrieve the query vars and store as variable $template 
    $template = $wp->query_vars;

    //pass the $template variable into the conditional statement and
    //check if the key \'test\' is one of the query_vars held in the $template array
    //and that \'test\' is equal to the value of the key which is set
    if ( array_key_exists( \'test\', $template ) && \'test\' == $template[\'test\'] ) {

        //if the key \'test\' exists and \'test\' matches the value of that key
        //then return the template specified below to handle presentation
        include( get_template_directory().\'/your-template-name-here.php\' );
        exit;
    }
}

结束

相关推荐