How to change the attachment url in Wordpress

2019-04-02 17:30发布

What I'm trying to do is change the link to the main attachment page in Wordpress. Basically, I'm trying to change the word attachment to media.

I'm trying to change:

example.com/parent-category/child-category/post-slug/attachment/attachment-name/

to:

example.com/parent-category/child-category/post-slug/media/attachment-name/

Thanks in advance for any help on this.

标签: wordpress
1条回答
叛逆
2楼-- · 2019-04-02 18:27

I know this is an old question, but I just stumbled on it and thought it was worth a shot;

function __filter_rewrite_rules( $rules )
{
    $_rules = array();
    foreach ( $rules as $rule => $rewrite )
        $_rules[ str_replace( 'attachment/', 'media/', $rule  ) ] = $rewrite;
    return $_rules;
}
add_filter( 'rewrite_rules_array', '__filter_rewrite_rules' );

function __filter_attachment_link( $link )
{
    return preg_replace( '#attachment/(.+)$#', 'media/$1', $link );
}
add_filter( 'attachment_link', '__filter_attachment_link' );
查看更多
登录 后发表回答