1

Quick Question for someone who knows their wordpress.

I'm using the following code to show the WC product gallery on the product page (in the right order).

Someone can quickly point me in the direction of including the image caption between the <p>'s?

global $product;
$attachment_ids = $product->get_gallery_attachment_ids();

echo '<div class="flexslider"><ul class="slides">';
foreach( $attachment_ids as $attachment_id ) 
{
echo '<li>';
  echo "<img src=".$image_link = wp_get_attachment_url( $attachment_id, 'large').">";
  echo '<p>';
  echo '</p>';
  echo '</li>';
}
echo '</ul></div>';
Miro Mitov
  • 21
  • 1
  • 3

2 Answers2

2

Here is the way to achieve it:

First of all add below code to your theme's functions.php :

function wp_get_attachment( $attachment_id ) {

    $attachment = get_post( $attachment_id );
    return array(
        'alt' => get_post_meta( $attachment->ID, '_wp_attachment_image_alt', true ),
        'caption' => $attachment->post_excerpt,
        'description' => $attachment->post_content,
        'href' => get_permalink( $attachment->ID ),
        'src' => $attachment->guid,
        'title' => $attachment->post_title
    );
}

After that you can use it like :

$attachment_meta = wp_get_attachment($attachment_id);
echo $attachment_meta['caption'];

So your final code would be :

global $product;
$attachment_ids = $product->get_gallery_attachment_ids();

echo '<div class="flexslider"><ul class="slides">';
foreach( $attachment_ids as $attachment_id ) 
{
    echo '<li>';
    echo "<img src=".$image_link = wp_get_attachment_url( $attachment_id, 'large').">";
    echo '<p>';
    $attachment_meta = wp_get_attachment($attachment_id);
    echo $attachment_meta['caption'];
    echo '</p>';
    echo '</li>';
}
echo '</ul></div>';

Source: wordpress topic forum

Rohil_PHPBeginner
  • 6,002
  • 2
  • 21
  • 32
  • Sorry to post in a thread almost a year old. I found this when searching for a solution to [my own problem](http://stackoverflow.com/questions/35985494/display-additional-product-information-e-g-image-caption-when-hovering-produc/35986804?noredirect=1#comment59629294_35986804). I tried inserting the above code into the WC content-product.php file, which output the product short description. What I want is the image caption set in WP backend. I only want it for product images in the product archive, not the single product page. Do I need to edit a different file, and in that case, which one? – ReddaJoppe Mar 15 '16 at 07:47
  • Above **function** will go into your theme's `functions.php` file. By using that function you can get all data like ALT, Caption etc. You need to pass `attachment ID` to get that data. Ex. If your attached product Image id is **12** then you can get data like `wp_get_attachment(12)`. Or else you can use plugin : [Product Description on Hover](https://wordpress.org/plugins/product-description-on-hover-woocommerce/) – Rohil_PHPBeginner Mar 15 '16 at 13:52
0

You can use wp_get_attachment_metadata. Something like:

$caption = wp_get_attachment_metadata( $attachment_id )['image_meta']['caption'];
M. Adam Kendall
  • 1,202
  • 9
  • 8
  • Thanks... but giving me a syntax error: 'Parse error: syntax error, unexpected '[' in /var/sites/t/website.co.uk/public_html/wp-content/plugins/shortcode-exec‌​-php/shortcode-exec-php-class.php(895) : eval()'d code on line 10' – Miro Mitov Apr 21 '15 at 17:03
  • This wont work because `wp_get_attachment_metadata` will give the [EXIF](http://en.wikipedia.org/wiki/Exchangeable_image_file_format) caption not the caption that you entered in the back end. – Rohil_PHPBeginner Apr 22 '15 at 13:18