gpt4 book ai didi

Wordpress add_rewrite_rule 给出 404

转载 作者:行者123 更新时间:2023-12-01 02:13:11 24 4
gpt4 key购买 nike

我想更改特定自定义帖子类型的永久链接结构。即

http://test.com/brand/calvin-klien?mtype=tab2
^this is dynamic


http://test.com/brand/calvin-klien/mtype/tab2
^this is dynamic

这是我试过的一段代码。

注册 add_rewrite_tag
function custom_rewrite_tag() {
add_rewrite_tag('%mtype%', '([a-z0-9\-]+)');
}
add_action('init', 'custom_rewrite_tag', 10, 0);

add_action('init', 'wpse50530_journal_archive_rewrite', 10, 0);

代码1
function wpse50530_journal_archive_rewrite(){

add_rewrite_rule('brand/([a-z0-9\-]+)/([a-z0-9\-]+)/$','index.php?name=$matches[1]/?mtype=$matches[2]','top');
}

代码2
add_action('generate_rewrite_rules', 'work_list');
function work_list($wp_rewrite) {
$newrules = array();
$newrules['brand/([a-z0-9\-]+)/([a-z0-9\-]+)/$'] = 'index.php?name=$matches[1]&mtype=$matches[2]';
$wp_rewrite->rules = $newrules + $wp_rewrite->rules;

以上两个我都试过了 codes , flushed permalinks但仍然是 404 .我不知道它为什么要创建 $匹配在 htaccess 中,因为 htacces 不知道是什么 $匹配

我也试过 monkeyman-rewrite-analyzer plugin这为我的永久链接显示了正确的匹配结果,但仍然是 wordpress 显示 404 .请参阅代码 1 和代码 2 的附加屏幕截图

Code1![][1] Code2

最佳答案

以下代码应该会有所帮助

add_action( 'init', 'so_27487849' );

function so_27487849() {

add_rewrite_rule(
'^brand/([^/]*)/mtype/([^/]*)/?',
'index.php?name=$matches[1]&mtype=$matches[2]',
'top');

}

刷新您的永久链接,它应该可以工作。

关于Wordpress add_rewrite_rule 给出 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27487849/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com