我需要创建一个前端页面(和子页面),该页面将显示保存在数据库中的自定义数据,但不是wordpress的一部分,它本身不是帖子或页面,而是API调用的数据。我不想使用短代码,因为需要子页面。
我已经查看了以下内容,但似乎无法按要求进行操作:Dynamic URL, not a physical page within the database
例如,我需要的是site.com/mypage/
但我不希望“mypage”存在于后端。
我通过以下方式实现了这一目标:
public function mypage_rewrite_ext() {
global $wp_rewrite;
$plugin_url = plugins_url( \'mypage.php\', __FILE__ );
$plugin_url = substr( $plugin_url, strlen( home_url() ) + 1 );
// The pattern is prefixed with \'^\'
// The substitution is prefixed with the "home root", at least a \'/\'
// This is equivalent to appending it to `non_wp_rules`
$wp_rewrite->add_external_rule( \'mypage$\', $plugin_url );
}
但我需要我的页面仍然在WordPress中,因为我需要使用页眉和页脚。我尝试了上述链接问题中的以下内容,但这只是重定向到主页(即使在重新保存永久链接之后):
public function add_query_vars( $query_vars )
{
$query_vars[] = \'test\';
return $query_vars;
}
public function add_endpoint()
{
add_rewrite_rule(\'^mypage/?\', \'index.php?__test=1\', \'top\');
flush_rewrite_rules(false); //// <---------- REMOVE THIS WHEN DONE
}
public function sniff_requests($wp_query)
{
global $wp;
if(isset($wp->query_vars[ \'__test\' ])) {
add_filter(\'template_include\', function ($original_template) {
// change the default template to a google map template
return plugin_dir_path( __FILE__ ) . \'mypage.php\';
});
}
}
关于1)我做错了什么和/或2)我如何做到这一点,有什么想法吗?