在Wordpress中自定义重写规则

Pen*_*e83 0 wordpress mod-rewrite rewrite

我在内部wordpress重写规则方面遇到了麻烦.我已经阅读了这个帖子,但我仍然无法得到任何结果:WordPress插件中的wp_rewrite

我解释一下我的情况:

1)我有一个名为'myplugin_template.php'的page_template与一个名为"mypage"的wordpress页面相关联.

<?php
get_header();
switch ($_GET['action']) {
  case = "show" {
  echo $_GET['say'];
  }
}
get_footer();
?>
Run Code Online (Sandbox Code Playgroud)

2)我需要为此链接创建重写规则:

HTTP://myblog/index.php页面名=我的空间和行动=展览会暨说=程序hello_world

如果我使用这个url所有的东西都没有问题,但我想实现这个结果:

http://myblog/mypage/say/hello_world/
Run Code Online (Sandbox Code Playgroud)

我真的不想破解我的.htaccess文件,但我不知道我怎么能用内部的wordpress重写器做到这一点.

The*_*dic 7

你需要添加自己的重写规则和查询变量 - 弹出这个functions.php;

function my_rewrite_rules($rules)
{
    global $wp_rewrite;

    // the slug of the page to handle these rules
    $my_page = 'mypage';

    // the key is a regular expression
    // the value maps matches into a query string
    $my_rule = array(
        'mypage/(.+)/(.+)/?' => 'index.php?pagename=' . $my_page . '&my_action=$matches[1]&my_show=$matches[2]'
    );

    return array_merge($my_rule, $rules);
}
add_filter('page_rewrite_rules', 'my_rewrite_rules');


function my_query_vars($vars)
{
    // these values should match those in the rewrite rule query string above
    // I recommend using something more unique than 'action' and 'show', as you
    // could collide with other plugins or WordPress core
    $my_vars = array(
        'my_action',
        'my_show'
    );

    return array_merge($my_vars, $vars);
}
add_filter('query_vars', 'my_query_vars');
Run Code Online (Sandbox Code Playgroud)

现在在您的页面模板中,替换$_GET[$var]get_query_var($var)这样;

<?php
get_header();
switch (get_query_var('my_action')) {
    case = "show" {
        echo esc_html(get_query_var('my_say')); // escape!
    }
}
get_footer();
?>
Run Code Online (Sandbox Code Playgroud)