将 Wordpress URL 路径转换为查询字符串 [英] Converting Wordpress URL path into query string

查看:32
本文介绍了将 Wordpress URL 路径转换为查询字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个接受查询字符串参数的 Wordpress 页面:

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

http://x.com/page-name/?parameter=value

这很好用.该页面正确获取 $_GET['parameter'] 的值.

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

我想要做的是可以将其作为普通 URL 输入:

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

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

我需要能够重写 URL,以便用户输入 URL 2,Wordpress 获取 URL 1.我使用的是 Apache,并且更喜欢使用 mod_rewrite 和 .htaccess 来实现.有什么建议吗?

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?

推荐答案

你可以在 WP 中使用你的主题的functions.php:

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;
}

在实施后重新保存您的永久链接设置.page 是用户访问此 URL 时要指向的页面的 slug (domain.com/page/remove/432),并且 $matches[1] 应该是 URL 中 remove/ 之后的数字.此数字可由稍后指定的变量访问,$query_vars[] = 'removeid';/目标页面模板上的 $removeid 将是 URL 中的数字(如果已指定).

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.

这篇关于将 Wordpress URL 路径转换为查询字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆