admin管理员组文章数量:1416302
the attachment page should have canonical added to the main post
on attachment page (image.php) I currently have (automatically added by All in One Seo Pack):
<link rel="canonical" href="" />
And I want to change it to point to the post (where this image is attached)
<link rel="canonical" href="" />
You can ignore All in One Seo Pack if that is difficutl to change, and maybe simple provide a way to add the canonical to the main post.
the attachment page should have canonical added to the main post
on attachment page (image.php) I currently have (automatically added by All in One Seo Pack):
<link rel="canonical" href="https://example/main-post/image-name" />
And I want to change it to point to the post (where this image is attached)
<link rel="canonical" href="https://example/main-post" />
You can ignore All in One Seo Pack if that is difficutl to change, and maybe simple provide a way to add the canonical to the main post.
Share Improve this question asked Jun 28, 2017 at 10:13 vyperlookvyperlook 1775 silver badges24 bronze badges1 Answer
Reset to default 2Here's an (untested) example where we inject into the header tag on attachment's pages, the canonical link of the attached post:
add_action( 'wp_head', 'wpse_attachment_parent_canonical' );
function wpse_attachment_parent_canonical()
{
// Only target attachment's pages
if( ! is_attachment() )
return;
$object = get_queried_object();
// Make sure we're dealing with a WP_Post object
if ( ! is_a( $object, '\WP_Post' ) )
return;
// Only target attachments that are attached to posts
if( 0 == $object->post_parent )
return;
// Output canonical link
printf(
'<link rel="canonical" href="%s" />' . PHP_EOL,
esc_url( get_permalink( $object->post_parent ) )
);
}
Note that we can't use the get_canonical_url
filter here to adjust the canonical url, as it's only applied to objects with publish post status. Attachments have inherit post status.
本文标签: imagesadd the post as canonical for attachment page wordpress
版权声明:本文标题:images - add the post as canonical for attachment page wordpress 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744682847a2619517.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论