我想知道捕获与模板不匹配的404的最佳代码部分。然而,在这一点上,我将检查url字符串,并输出我自己的动态html,如果它与模式匹配。它是动态内容,因为wordpress后端中没有它的内容。因此,我希望回显html并在匹配时退出。如果没有,则继续,404正常。
关于这个问题。Dynamic URL generates dynamic content
泰
我想知道捕获与模板不匹配的404的最佳代码部分。然而,在这一点上,我将检查url字符串,并输出我自己的动态html,如果它与模式匹配。它是动态内容,因为wordpress后端中没有它的内容。因此,我希望回显html并在匹配时退出。如果没有,则继续,404正常。
关于这个问题。Dynamic URL generates dynamic content
泰
不可能在WP中捕捉到与模板不匹配的内容,因为它将始终与一些模板匹配。它将回退到的最后一个模板就是index.php
, 模板层次结构中最通用的一个。
通常,此类逻辑的适当挂钩通常是template_redirect
在里面template-loader.php
. 此时查询已经完成,条件上下文已经完全设置好,但本机模板解析和包含尚未启动。
你可以404.php 文件这是作为主题的一部分的普通文件。您可以在其中处理所有逻辑,并让它加载动态html。
OR
您可以在函数中使用如下函数。php文件。我刚刚测试过,您可以使用var_dump()
放置在此函数中。// borrowed from http://wordpress.stackexchange.com/a/1883/10907
function stoi2m1_404_override() {
global $wp_query;
// return if not a 404 page
if (!$wp_query->is_404)
return;
// dump the data in the query, parameters from the URL
echo \'<pre>\';
var_dump($wp_query->query);
echo \'</pre>\';
/* resulted in something like this for me, Im using a custom post type movies
array(4) {
["page"]=>
string(0) ""
["movies"]=>
string(4) "missing"
["post_type"]=>
string(6) "movies"
["name"]=>
string(4) "missing"
}*/
if ($wp_query->query[\'name\'] == \'missing\' ) { //what ever name/title of post you are looking for
status_header( 200 ); // set the appropriate header
$wp_query->is_404=false; // set is_404 to false
// apply any more logic you need here
// or include custom template file here
// you will want to keep the usual template from loading
}
}
add_filter(\'template_redirect\', \'stoi2m1_404_override\' );