首页 > 解决方案 > WordPress add_rewrite_rule() 显示 404 not found

问题描述

在 wordPress 中,我创建了一个名为 compare 的页面。因此,当我从浏览器 url 访问该页面时,我正在获取内容。(http://localhost/task/compare/)

add_action( 'init', 'custom_rewrite_rules');
function custom_rewrite_rules() {
    add_rewrite_rule('^compare/([0-9]+)/?', 'index.php?page_id=250', 'top'); // Here 250 is the page id for compare
    flush_rewrite_rules();
}

因此,当我尝试访问 url 中的页面时

http://localhost/task/compare/aa-bb-cc-dd

Its showing 404 not found error.

那么有人可以告诉我我在这里做错了什么吗?欢迎任何帮助和建议。

标签: wordpress

解决方案


在我看来,您应该尝试刷新永久链接。请参阅下面的链接。

https://typerocket.com/flushing-permalinks-in-wordpress/


推荐阅读