2010-07-01 104 views
0

我遇到了内部wordpress重写规则的问题。 我读过这个线程,但我仍然不能得到任何结果:wp_rewrite in a WordPress PluginWordpress中的自定义重写规则

我解释一下我的情况:

1)我已经叫page_template关联称为一个WordPress页面“myplugin_template.php”“我的页面”。

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

2)我需要为这个链接重写规则:如果我使用这个网址的所有的东西工程没有问题

http://myblog/index.php?pagename=mypage&action=show&say=hello_world

,但我想实现这个结果:

http://myblog/mypage/say/hello_world/ 

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

回答

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'); 
在你的页面模板

现在,随着get_query_var($var)像这样更换$_GET[$var];

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

该规则不应该是'mypage /([^ /] +)/([^ /] +)/?'? – 2013-12-06 20:05:57