【问题标题】:Rewrite everything after url pattern + htaccess在 url 模式 + htaccess 之后重写所有内容
【发布时间】:2016-06-02 21:57:48
【问题描述】:

我目前正在从服务中加载一些作业,并且我已经通过这种方式构建了 url:

mysite.com/job-detail/job-title

网址中的job-detail 将始终保持不变,而job-title 将取决于API 中的内容。

由于我使用的是 Wordpress,我想确保所有 /job-detail/xxx 页面都使用相同的模板,以便我可以根据需要显示我的数据。

有什么想法吗?

【问题讨论】:

    标签: javascript php wordpress .htaccess url-rewriting


    【解决方案1】:

    我自己找到了解决方案:

    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;
    
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-14
      • 2019-09-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多