Javascript 在url模式之后重写所有内容+;htaccess
我目前正在从一个服务加载一些作业,并通过以下方式构建url:Javascript 在url模式之后重写所有内容+;htaccess,javascript,php,wordpress,.htaccess,url-rewriting,Javascript,Php,Wordpress,.htaccess,Url Rewriting,我目前正在从一个服务加载一些作业,并通过以下方式构建url: mysite.com/job-detail/job-title url中的作业详细信息将始终保持不变,而作业标题将取决于API中的内容 因为我使用的是Wordpress,所以我想确保所有的/job detail/xxx页面都使用相同的模板,这样我就可以随心所欲地显示我的数据 有什么想法吗 我自己找到了解决方案: add_action('init', 'fake_page_rewrite'); function fake_page_
mysite.com/job-detail/job-title
url中的作业详细信息
将始终保持不变,而作业标题
将取决于API中的内容
因为我使用的是Wordpress,所以我想确保所有的/job detail/xxx
页面都使用相同的模板,这样我就可以随心所欲地显示我的数据
有什么想法吗 我自己找到了解决方案:
add_action('init', 'fake_page_rewrite');
function fake_page_rewrite(){
global $wp_rewrite;
//set up our query variable %fake_page% which equates to index.php?fake_page=
add_rewrite_tag( '%fake_page%', '([^&]+)');
//add rewrite rule that matches /blog
add_rewrite_rule('^job-detail/?','index.php?fake_page=themes/subjects','top');
//add endpoint, in this case 'blog' to satisfy our rewrite rule /blog, /blog/page/ etc..
add_rewrite_endpoint( 'job-detail', EP_PERMALINK | EP_PAGES );
//flush rules to get this to work properly
$wp_rewrite->flush_rules();
}
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 'fake_page' is one of the query_vars held in the $template array
//and that 'blog' is equal to the value of the key which is set
if ( array_key_exists( 'fake_page', $template ) && 'themes/subjects' == $template['fake_page'] ) {
//if the key 'fake_page' exists and 'blog' matches the value of that key
//then return the template specified below to handle presentation
include( get_stylesheet_directory().'/template-job.php' );
exit;
}
}