Take the 2-minute tour ×
WordPress Development Stack Exchange is a question and answer site for WordPress developers and administrators. It's 100% free, no registration required.

I'm currently coding a theme and I'm wanting to use the gallery post format so I've created a file called content-gallery.php and have begun editing.

I don't want to use the HTML that Wordpress returns from 'get_post_gallery) so I've got it to return the data instead.

Then I loop through, displaying the image on each iteration. My problem comes from the returned data, there doesn't appear to be any alt text for the image. What's the best way to get the alt text?

The best I can think is to split the IDs returned from the gallery and use the ID in a function call to get the image alt using wp_get_attachment_image();

Is this the best way to do it or is there something obvious I've missed?

My code is as follows:

<article id="post-<?php the_ID(); ?>" <?php post_class(); ?>>

<h1><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h1> <?php

$gallery = get_post_gallery( get_the_ID(), false );

$ids = explode(',', $gallery['ids']);

foreach($ids as $id):
    echo wp_get_attachment_image($id);
endforeach; ?>

    <p>Posted by <?php the_author_link(); ?> <time datetime="<?php the_time('Y-m-d'); ?>"><?php the_date(); ?> <?php the_time('g:i a'); ?></time></p> <?php
    edit_post_link('Edit'); ?>


share|improve this question

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.