如何在Wordpress中更改附件URL

时间:2010-12-26 02:23:51

标签: wordpress

我要做的是更改Wordpress中主要附件页面的链接。基本上,我正在尝试将附件一词改为媒体。

我想改变:

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

为:

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

提前感谢您提供任何帮助。

1 个答案:

答案 0 :(得分:3)

我知道这是一个老问题,但我偶然发现它并认为值得一试;

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' );