开发者

Converting Wordpress URL path into query string

开发者 https://www.devze.com 2023-02-02 22:19 出处:网络
I have a Wordpress page that accepts a query string argument: http://x.com/page开发者_Python百科-name/?parameter=value

I have a Wordpress page that accepts a query string argument:

http://x.com/page开发者_Python百科-name/?parameter=value

This works fine. The page gets the value of $_GET['parameter'] correctly.

What I want to do is make it possible to type this as a normal URL:

http://x.com/page-name/value

I need the ability to rewrite the URL so the user enters URL 2, and Wordpress gets URL 1. I am using Apache and would prefer to do this with mod_rewrite and .htaccess. Any advice?


You can do this within WP using your theme's functions.php:

add_action( 'init', 'ss_permalinks' );
function ss_permalinks() {
    add_rewrite_rule(
        'page/remove/([^/]+)/?',
        'index.php?pagename=page&service=$matches[1]',
        'top'
);
}
add_filter( 'query_vars', 'ss_query_vars' );
function ss_query_vars( $query_vars ) {
    $query_vars[] = 'removeid';
    return $query_vars;
}

Re-save your permalink settings once after implementing. page is the slug of the page to point to when the user access this URL (domain.com/page/remove/432), and $matches[1] should be the number after remove/ in the URL. This number is accessible by the variable specified later, $query_vars[] = 'removeid';/ $removeid on the target page's template will be the number in the URL, if specified.

0

精彩评论

暂无评论...
验证码 换一张
取 消