1

使用永久链接的 WP 编码人员将熟知以下代码块。

add_filter('rewrite_rules_array', array($this, 'insert_rewrite_rules'));
function insert_rewrite_rules($rules)
{
    // $link_cloaking_text is properly set

    $newrules = array();
    $newrules['([^\/]*)/\b'.$link_cloaking_text.'\b/(\d*)$'] =    
    'index.php?pagename=$matches[1]&link_cloaking_id=$matches[2]';

    return $newrules + $rules;
}

问题:我无法获取pagenamelink_cloaking_id值(query_vars钩子并$wp_rewrite->flush_rules()正确使用)

$newrules['([^\/]*)/\b'.$link_cloaking_text.'\b/(\d*)$'] =     
'index.php?pagename=$matches[1]&link_cloaking_id=$matches[2]';

debug:
    WP_Query::__set_state(array(
       'query_vars' => 
      array (
        'error' => '404',
        'm' => 0,
        'p' => 0,
        'post_parent' => '',
        'subpost' => '',
        'subpost_id' => '',
        'attachment' => '',
        'attachment_id' => 0,
        'name' => '',
        'static' => '',
        'pagename' => '', // empty
        'page_id' => 0,

但是如果我$matches[1]用一个常量替换my_pagename,那么就可以了:

$newrules['([^\/]*)/\b'.$link_cloaking_text.'\b/(\d*)$'] =      
'index.php?pagename=my_pagename&link_cloaking_id=$matches[2]';

debug:
    WP_Query::__set_state(array(
       'query_vars' => 
      array (
        'pagename' => 'my_pagename', // correct
        'link_cloaking_id' => '8', // correct
        'error' => '',
        'm' => 0,
        'p' => 0,
        'post_parent' => '',

我究竟做错了什么??

4

0 回答 0