admin管理员组文章数量:1323330
I have a problem understanding the behavior of the Attachment Page Permalinks Into Setting-->Permalinks I have set / as the preferred setting.
When I upload an image a permalink for the Attachment Page is automatically created. Example:
- I'm into Service (a page) and I upload landscape.jpg to the media gallery.
- The permalink / is automatically created
- If I want to create a page at named landscape with services as parent I can't
- When i create the page Landscape wordpress rename it example/services/landscape-2/
Is there a way to modify the way WordPress handles the permalinks of Attachment Pages that are automatically created when we upload the images to the media gallery?
- Can we modify all the Attachment Page having into the permalink something like /img-upload/ so wordpress will not get confused ?
- Is it possiible to revert to the old style permalink handling just for the attachment pages? Before with the permalink of the attachment page having this kind of url example/?attachment_id=37 we had no this problem.. and for the seo we were just redirecting all the attachment page url to the page that was containing the image; solving in this way the duplicated content issue that was giving the creation of a page with Title description and image...
Thanks in advance!
I have a problem understanding the behavior of the Attachment Page Permalinks Into Setting-->Permalinks I have set http://www.example/sample-post/ as the preferred setting.
When I upload an image a permalink for the Attachment Page is automatically created. Example:
- I'm into Service (a page) and I upload landscape.jpg to the media gallery.
- The permalink http://www.example/services/landscape/ is automatically created
- If I want to create a page at named landscape with services as parent I can't
- When i create the page Landscape wordpress rename it example/services/landscape-2/
Is there a way to modify the way WordPress handles the permalinks of Attachment Pages that are automatically created when we upload the images to the media gallery?
- Can we modify all the Attachment Page having into the permalink something like /img-upload/ so wordpress will not get confused ?
- Is it possiible to revert to the old style permalink handling just for the attachment pages? Before with the permalink of the attachment page having this kind of url example/?attachment_id=37 we had no this problem.. and for the seo we were just redirecting all the attachment page url to the page that was containing the image; solving in this way the duplicated content issue that was giving the creation of a page with Title description and image...
Thanks in advance!
Share Improve this question asked Jul 29, 2016 at 3:58 WilliamWilliam 211 silver badge2 bronze badges2 Answers
Reset to default 2To provide an answer to your first question and for future reference to tackle the problem you described with the slug already being in use when you first uploaded an attachment to the same parent. The following code will get rid of this 'annoying' thing (annoying in some use cases).
This code rewrites the slug of the attachment upon uploading to add a prefix (or suffix) to the slug. In that case you have less chance of getting in the way of page slugs in the future.
add_action('add_attachment', function($postId) {
// get the attachment post object
$attachment = get_post($postId);
// get the slug for the attachment
$slug = $attachment->post_name;
// update the post data of the attachment with an edited slug
wp_update_post(array(
'ID' => $postId,
'post_name' => 'media-'.$slug, //adds a prefix
//'post_name' => $slug.'-photo', //adds a suffix
));
});
I hope this will help anyone.
The "attachment page" is just a post type, like "page" or "post" are.
The slug in the URL for each post must be unique, shared slug is only permitted accross different post types (since WordPress 4.1), but with some limitations.
One obvious limitation is that same slug can not be allowed for posts which have the same post parent. If the post parent is the same, the URL path will be the same and the post slug will be in the same level in that path, which is obviously a problem: same path and slug would result in the same URL for the two different contents. That is why WordPress adds -2
to one of the them and fix this issue.
The situation described above is what is happening to you. You are trying to create two posts of different post types that share post parent and URL path.
Even you use a different URL structure for pages and attachments, WordPress won't allow same slug in both if they share the same parent post.
So the best solution is to give each post, attachment and page, different names and slugs. URL manipulation is quite more complicated and won't give you the desired SEO benefits you think it will give you.
That is my opinion from a SEO perspective. Using different slugs, for example example/services/landscape-photo/
for attachement page, won't hurt your SEO at all.
In the other hand, redirecting the attachment page, as you are doing, may actually hurt your SEO. I assume you are doing it with 3xx status code which means "Content moved for xx reason". For example, 301 code means "Content moved permanently to a new location".
If you use 3xx status code to redirect attachment page to parent page, you are doing it wrong. Simply because you have not moved the content, the content of the URL where you are redirecting to is not the same content of the URL where you are redirecting from, so there is not duplicated content and there is not moved content. That is why the redirection is incorrect from a SEO perspective.
If you don't want attachement pages being indexed by search engines, I would use robots meta tag with noindex
value. Other methods can be used to avoid attachment be indexed and avoid fake redirections, it depends on the exact situation.
本文标签: Attachment Page PermalinkURL Rewrite Issue How to change the actual rewriting
版权声明:本文标题:Attachment Page PermalinkURL Rewrite Issue. How to change the actual rewriting? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742139833a2422532.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论