Answer the question
In order to leave comments, you need to log in
How to display the number (exactly the number) of the Woocommerce product gallery image?
Hello, dear forum users! I had a task to display in the gallery of a single product alt at the image of the product in the form "Product name + Photo (x)", where x is the number of the image in the product gallery. Site on Woocommerce.
I got the name, in principle, quickly
add_filter('wp_get_attachment_image_attributes', 'change_attachement_image_attributes', 20, 2);
function change_attachement_image_attributes($attr, $attachment) {
global $post;
if ($post->post_type == 'product') {
$num = "Номер картинки";
$title = $post->post_title;
$attr['alt'] = sprintf(" %s Фото - %s.",$title,$num);
$attr['title'] = $title;
}
return $attr;
}
<a href="https://codex.wordpress.org/%D0%A1%D0%BF%D1%80%D0%B0%D0%B2%D0%BE%D1%87%D0%BD%D0%B8%D0%BA_%D0%BF%D0%BE_%D1%84%D1%83%D0%BD%D0%BA%D1%86%D0%B8%D1%8F%D0%BC/get_post"></a>
, but I did not find anything in the list of returned fields for the result I needed. Tell me, please, all the brains have already broken. add_filter('wp_get_attachment_image_attributes', 'change_attachement_image_attributes', 20, 2);
function change_attachement_image_attributes($attr, $attachment) {
global $post;
if ($post->post_type == 'product') {
$title = $post->post_title;
static $num = 0;
$num++;
$attr['alt'] = sprintf("Фото %d - %s.",$num,$title);
}
return $attr;
}
Answer the question
In order to leave comments, you need to log in
Create a static variable inside the function and increment it by one in every call
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question