2026-01-31 15:53:34 严潇健
评论(0)
add_action('init', function() {
add_action('template_redirect', function() {
if (is_attachment()) {
global $post;
if ($post && $post->post_parent) {
wp_redirect(get_permalink($post->post_parent), 301);
} else {
wp_redirect(home_url(), 301);
}
exit;
}
});
add_filter('attachment_link', function($url, $post_id) {
return wp_get_attachment_url($post_id);
}, 99, 2);
add_action('wp_head', function() {
if (is_attachment()) {
echo '<meta name="robots" content="noindex,nofollow" />';
}
}, 1);
add_filter('rewrite_rules_array', function($rules) {
foreach ($rules as $rule => $rewrite) {
if (strpos($rewrite, 'attachment=') !== false) {
unset($rules[$rule]);
}
}
return $rules;
});
}, 1);
评论前必须登录!
注册