Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Wordpress中的自定义重写规则_Wordpress_Mod Rewrite_Rewrite - Fatal编程技术网

Wordpress中的自定义重写规则

Wordpress中的自定义重写规则,wordpress,mod-rewrite,rewrite,Wordpress,Mod Rewrite,Rewrite,我遇到了wordpress内部重写规则的麻烦。 我读过这篇文章,但仍然没有得到任何结果: 我解释我的情况: 1) 我有一个名为“myplugin\u template.php”的页面模板与一个名为“mypage”的wordpress页面相关联 我真的不想破解我的.htaccess文件,但我不知道如何使用内部wordpress重写器来实现这一点。您需要添加自己的重写规则和查询变量-将其放入functions.php function my_rewrite_rules($rules) { g

我遇到了wordpress内部重写规则的麻烦。 我读过这篇文章,但仍然没有得到任何结果:

我解释我的情况:

1) 我有一个名为“myplugin\u template.php”的页面模板与一个名为“mypage”的wordpress页面相关联


我真的不想破解我的.htaccess文件,但我不知道如何使用内部wordpress重写器来实现这一点。

您需要添加自己的重写规则和查询变量-将其放入
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');
现在在您的页面模板中,将
$\u GET[$var]
替换为
GET\u query\u var($var)
,如下所示

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

规则不应该是
mypage/([^/]+)/([^/]+)/?
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');
<?php
get_header();
switch (get_query_var('my_action')) {
    case = "show" {
        echo esc_html(get_query_var('my_say')); // escape!
    }
}
get_footer();
?>