让woocommerce将产品标题设置为产品图片的标题和alt标签

woocommerce图像标题和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;
}

这个是不完美的,在相册图不显示ALT标签,更完美的方法是SQL批量更新。
登录到Phpmyadmin,选择数据库并运行此查询以更新图像标题:
UPDATE wp_posts as a
LEFT JOIN wp_postmeta as b ON a.ID = b.meta_value
LEFT JOIN wp_posts as c ON b.post_id = c.ID
SET a.post_title = c.post_title
WHERE a.post_type = 'attachment'
AND a.post_mime_type = 'image/jpeg'
AND b.meta_key = '_thumbnail_id'
AND c.post_type = 'product'

以及此SQL查询以更新图像的alt标签:
UPDATE wp_postmeta as z 
LEFT JOIN wp_posts as a ON z.post_id = a.ID 
LEFT JOIN wp_postmeta as b ON a.ID = b.meta_value 
LEFT JOIN wp_posts as c ON b.post_id = c.ID 
SET z.meta_value = c.post_title 
WHERE z.meta_key = '_wp_attachment_image_alt' 
AND a.post_type = 'attachment' 
AND a.post_mime_type = 'image/jpeg' 
AND b.meta_key = '_thumbnail_id' 
AND c.post_type = 'product'




相关文章

  1. 让woocommerce将产品标题设置为产品图片的

    woocommerce图像标题和alt标签对SEO至关重要,更改每个图像标题和alt标签将花费很长时间。一般优化我们会在函数当中加载代码: add_filter(wp_get_attachment_image_attributes, change_attachement_image_a...

    2020-02-29
  2. 阻止恶意URL请求错误查询BBQ脚本代码

    阻止恶意URL请求错误查询BBQ是一个简单的脚本,可保护您的网站免遭恶意URL请求。检查所有传入流量,并悄阻止掉一些恶意请教代码eval(,base64_和过长请求字符串。这是一个简单而可靠...

    2020-04-13