dream8877 2018-01-01 11:09
浏览 88
已采纳

动态创建wp页面

I'm using a custom page template that is intended to create a dynamic content using the wpdb class and some external tables within the database. I'm trying to keep the url's structure as clean as possible, hence im using the 'post name' option in the permalinks..

I'll just give the whole flow so it won't be confusing.

  1. An user is trying to reach www.domain.com/dynamic/347
  2. The template is searching for '347' in the database and creates a special dedicated page
  3. All the rendering is being executed within the template itself (the one that the /dynamic page is using)

The problem is with the mod_rewrite - Since I'm using the post_name option in wp's permalinks - the wordpress itself is trying to access an actual page called '347' under the parent of 'dynamic', instead of rendering dynamic's template and use 347 as a parameter, by 'exploding' the url and retrieve the last value as the dynamic parameter, like this -

$str = "$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$dynamic_para = explode("/", $str, 4); // in this case - 347
//... retrieve dynamic_para from db and stuff with it.

How can I bypass wp's current mod_rewrite specifically for this page? Is it even possible? I am trying to avoid sending GET variables with the url, e.g. www.domain.com/dynamic/?para=347

  • 写回答

1条回答 默认 最新

  • duanjue2560 2018-01-01 12:48
    关注

    want is /dynamic/xxx will be rewritten to /dynamic?para=xxx

    in WP you should rewrite to index.php with querystring (user still sees "/dynamic/xxx" in their browser address bar)

    where exactly did you place the tc_query_vars_filter and the tc_rewrite_rules($rules) functions

    You can place these in the (child) theme's functions.php or better create add them to your own simple plugin so they will work with any theme:

    e.g. create, upload, and activate:

    <?php
    /*
    Plugin Name: Theme independent functions
    Description: NOT TESTED
    */
    
    //  allow WP to store querystring attribs for use in our pages
    function your_query_vars_filter($vars) {
      $vars[] = 'para';
      return $vars;
    }
    add_filter( 'query_vars', 'your_query_vars_filter' );
    
    //  "rewrite" /dynamic/xxx to index.php?pagename=dynamic&para=xxx
    function your_rewrite_rules($rules) {
       global $wp_rewrite;
       $your_rule = array( // (not tested)
         'dynamic/(.+)/?' => 'index.php?pagename=dynamic&para=$matches[1]'
       );
       return array_merge($your_rule, $rules);
    }
    add_filter('page_rewrite_rules', 'your_rewrite_rules');
    
    // any other site customisation stuff you want to add to this plugin
    ?>
    

    N.B. above filter is for Pages; for POSTS use post_rewrite_rules filter instead

    Then in your custom page template:

    $my_search_var = get_query_var('para'); // sanitize as reqd

    Wordpress "caches" its re-write rules so finally we need to flush them so your new ones will be recognised:

    The usual advice is you can flush by simply clicking "Save Changes" button on admin dashboard Permalink page (Settings->Permalinks) However, (either due to a quirk on my site or a change in the latest version of WP?) last time I did this I actually had to edit the permalink, save the change then change it back to what it should be, and save again for my rewrite functions rules to be applied.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 (标签-UDP|关键词-client)
  • ¥15 关于库卡officelite无法与虚拟机通讯的问题
  • ¥15 qgcomp混合物线性模型分析的代码出现错误:Model aliasing occurred
  • ¥100 已有python代码,要求做成可执行程序,程序设计内容不多
  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答
  • ¥20 在本地部署CHATRWKV时遇到了AttributeError: 'str' object has no attribute 'requires_grad'