admin管理员组文章数量:1426094
My attachments are currently rewritten from
http://localhost/?attachment_id=3
to
http://localhost/images/image-title
using @Bainternet's answer here.
$new_rules['images/(\d*)$'] = 'index.php?attachment_id=$matches[1]';
However, wordpress still refers to the link as the default http://localhost/?attachment_id=3
. Wordpress functions such as the_permalink, get_attachment_url, get_attachment_image_src, etc. all use the default format of http://localhost/?attachment_id=3
.
I am able to access the image as intended if I manually type in the rewritten format http://localhost/images/image-title
.
How can I get wordpress to use my custom rewrite for the permalink especially on the admin page?
EDIT: reworded question for specificity and correctness:
How do I override the_permalink
to use the format /images/image-title
instead of /?attachment_id=ID
?
I can get image-title using $post->post_title
.
EDIT #2:
For anyone reading this question in the future, I found it best to use $post->post_name
for the link to insure uniqueness.
/images/post_title
My attachments are currently rewritten from
http://localhost/?attachment_id=3
to
http://localhost/images/image-title
using @Bainternet's answer here.
$new_rules['images/(\d*)$'] = 'index.php?attachment_id=$matches[1]';
However, wordpress still refers to the link as the default http://localhost/?attachment_id=3
. Wordpress functions such as the_permalink, get_attachment_url, get_attachment_image_src, etc. all use the default format of http://localhost/?attachment_id=3
.
I am able to access the image as intended if I manually type in the rewritten format http://localhost/images/image-title
.
How can I get wordpress to use my custom rewrite for the permalink especially on the admin page?
EDIT: reworded question for specificity and correctness:
How do I override the_permalink
to use the format /images/image-title
instead of /?attachment_id=ID
?
I can get image-title using $post->post_title
.
EDIT #2:
For anyone reading this question in the future, I found it best to use $post->post_name
for the link to insure uniqueness.
/images/post_title
- 1 that rule works with the attachment ID, how do you get the title from that? – Milo Commented Apr 27, 2014 at 16:59
2 Answers
Reset to default 6 +50Your rule works with the attachment ID, so I'm not sure how you're using the title, but the answer is almost identical in either case. The filter you want is attachment_link
:
function wpd_attachment_link( $link, $post_id ){
$post = get_post( $post_id );
return home_url( '/images/' . $post->post_title );
}
add_filter( 'attachment_link', 'wpd_attachment_link', 20, 2 );
Change $post->post_title
to $post->ID
to put the ID in the URL instead of title.
Some other way to Change the permalink url
function ro_remove_attachment_title_caption( $attach_id ){
$args = array(
'ID' => $attach_id,
'post_name' => "permalinkName",
);
wp_update_post( $args );
}
add_action( 'add_attachment', 'ro_remove_attachment_title_caption', 10, 1 );
It works in the context of the Plugin: remove OLYMPUS DIGITAL CAMERA from caption and title by Image upload
本文标签: seoChange the Permalink for wordpress attachment
版权声明:本文标题:seo - Change the Permalink for wordpress attachment 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745463123a2659414.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论