【问题标题】:Change post URL with WordPress add_rewrite_rule()使用 WordPress add_rewrite_rule() 更改帖子 URL
【发布时间】:2021-10-13 01:23:58
【问题描述】:

我的帖子的 URL 如下:

http://example.com/news/postname

wp-admin 中的“自定义结构”设置如下:

http://example.com[/news/%postname%]

我想以我的帖子开头的帖子名称的 URL(例如 mypost-postname)更改为

http://example.com/my-news/postname

我尝试像这样使用 add_rewrite_rule:

add_action( 'init', 'new_posts_with_new_url' );
function new_posts_with_new_url()
{
    add_rewrite_rule('my-news/([^/]*)/?','index.php?pagename=news/mypost-$matches[1]','top');
}

但它似乎不起作用。

我做错了哪一步?非常感谢。

【问题讨论】:

标签: wordpress url-rewriting add rules


【解决方案1】:

在我的情况下,index.php?pagename=news/mypost-$matches[1] 错误,

您可以使用echo add_query_arg( $wp->query_vars, home_url() );查看真实网址。

【讨论】:

    猜你喜欢
    • 2015-01-27
    • 1970-01-01
    • 2020-07-04
    • 2021-09-03
    • 2016-07-13
    • 1970-01-01
    • 2016-06-10
    • 1970-01-01
    • 2017-03-30
    相关资源
    最近更新 更多