本文介绍了如何更改 Wordpress 中的附件 url的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

我正在尝试改变:

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

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

到:

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

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

在此先感谢您对此的任何帮助.

Thanks in advance for any help on this.

推荐答案

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

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

这篇关于如何更改 Wordpress 中的附件 url的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 04:54