【发布时间】:2022-03-19 04:15:26
【问题描述】:
在做了一些研究后,我发现图片标题和 alt 对 SEO 很重要,更改每个图片标题和 alt 需要很长时间。
我在这里找到了这段代码,但它不会影响当前的图像。
add_filter('wp_get_attachment_image_attributes', 'change_attachement_image_attributes', 20, 2);
function change_attachement_image_attributes( $attr, $attachment ){
// Get post parent
$parent = get_post_field( 'post_parent', $attachment);
// Get post type to check if it's product
$type = get_post_field( 'post_type', $parent);
if( $type != 'product' ){
return $attr;
}
/// Get title
$title = get_post_field( 'post_title', $parent);
$attr['alt'] = $title;
$attr['title'] = $title;
return $attr;
}
【问题讨论】:
标签: php wordpress woocommerce