php - 自定义模板的 WordPress URL 重写

标签 php wordpress .htaccess url-rewriting

我有一个网址

http://classexample.com/student-details/?cname=btech&sname=xyz

我想将其转换为

http://classexample.com/student-details/btech/xyz

并且还想要使用 php 的值 btech 和 xyz

我已经尝试过

function custom_rewrite_basic() {
   add_rewrite_rule('^student-details/([\w+]*)/([\w+]*)/', 'index.php/?pagename=student-details&cname=$matches[1]&sname=$matches[2]', 'top');
}
add_action('init', 'custom_rewrite_basic');

但未能成功

任何帮助将不胜感激!

最佳答案

与此一起使用

function custom_rewrite_tags() {
      add_rewrite_tag('%cname%', '([^&]+)');
      add_rewrite_tag('%sname%', '([^&]+)');
    }
add_action('init', 'custom_rewrite_tags', 10, 0);

function custom_rewrite_basic() {
   add_rewrite_rule('^student-details/([\w+]*)/([\w+]*)/', 'index.php/?pagename=student-details&cname=$matches[1]&sname=$matches[2]', 'top');
}
add_action('init', 'custom_rewrite_basic');

请重置永久链接,因为保存永久链接后 htaccess 文件已更新

关于php - 自定义模板的 WordPress URL 重写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41358381/

相关文章:

php - 将条件数组传递给 doctrine expr()->orx() 方法

javascript - $jquery.post() PHP 和 Mysql

php - 我该如何设置 smarty 模板?

php - 在 WooCommerce 结帐中为特定选择的运输方式添加主体类

.htaccess - RewriteRule接受2个参数

php - htaccess 创建集中的 php 文件

php - error_reporting 无法正常显示 fatal error

jquery - 从字符串 jQuery 中提取前 n 个单词,jQuery 中的 WordPress 样式摘录(x 单词数)

html - 从一页中删除顶部栏/标题/主导航链接

php - 如何使用 htaccess 来做到这一点?