Prevent Duplicate Image Attributes

When a post / product has multiple images, it’s possible for all the images to have the same image attribute.

For example, if Image Attributes Pro is configured to use the product title as image attribute, then all images in the product will have the same product title.

From a technical stand point, duplicate image attributes is not an issue for SEO. However from an accessibility / user experience stand point it is odd to have the same image attribute for all images in a post.

The most commonly asked solution for this problem is to add a count to the image attributes so each of them would be unique. With a count added the image attributes would read “product title 1”, “product title 2” and so on.

As of Image Attributes Pro version 3.2, this is quite complicated to accomplish and there is no %count% custom image attribute tag.

However, here are some ideas to solve the problem of duplicate image attributes.

Use A Different Category Name For Each Image

Image Attributes Pro comes with a custom attribute tag %category% which will add the first category name to the image attribute. If the post / product has multiple categories, this attribute tag can be modified to return a different category for each image.

To do so, add the following code into the functions.php of your active theme. This code takes advantage of the iaffpro_custom_attribute_tag_category_names filter and returns a random category name for each image.

/**
 * Return a random category name from the categories associated with a post / product.
 * This modifies the output generated by %category% custom attributes tag in Image Attributes Pro.
 * 
 * @link https://imageattributespro.com/codex/iaffpro_custom_attribute_tag_category_names/
 * 
 * @param $first_category (string) The first category available.
 * @param $categories (array) Contains the names of all categories associated with given post.
 * 
 * @return (string) A random category name.
 */
function prefix_iap_add_random_category_in_custom_attribute_tag_category( $first_category, $categories ) {

    $random_key = array_rand( $categories );

    // Return a random category.
    return $categories[$random_key];
}
add_filter( 'iaffpro_custom_attribute_tag_category_names', 'prefix_iap_add_random_category_in_custom_attribute_tag_category', 10, 2 );

Use A Different Tag Name For Each Image

Another option is to use a random tag for each image in a post / product. The default behaviour of the %tag% custom image attribute is to use the first tag name available.

This can be modified to output a random tag name from the tags used in the post / product by adding the following code to functions.php of your active theme. Here we are using the iaffpro_custom_attribute_tag_tag_names filter to return a random tag name for each image.

/**
 * Return a random tag name from the tags associated with a post / product.
 * This modifies the output generated by %tag% custom attributes tag in Image Attributes Pro.
 * 
 * @link https://imageattributespro.com/codex/iaffpro_custom_attribute_tag_tag_names/
 * 
 * @param $first_tag (string) The first tag available.
 * @param $tags (array) Contains the names of all tags associated with given post.
 * 
 * @return (string) A random tag name.
 */
function prefix_iap_add_random_tag_in_custom_attribute_tag_tag( $first_tag, $tags ) {

    $random_key = array_rand( $tags );

    // Return a random tag.
    return $tags[$random_key];
}
add_filter( 'iaffpro_custom_attribute_tag_tag_names', 'prefix_iap_add_random_tag_in_custom_attribute_tag_tag', 10, 2 );
Was this article helpful?
Yes, thanks! 👍Not really 👎