V
V
Valera Udav2014-02-24 18:39:40
WordPress
Valera Udav, 2014-02-24 18:39:40

How to display photo description in WordPress?

The gallery on the site is formed as follows

<?php
if( has_shortcode( $post->post_content, 'gallery' ) ) {
  $gallery = get_post_gallery_images( $post->ID );

  foreach( $gallery as $image ) {
    ?>
    <div class="gallery-box-img">
      <div class="gallery-img">
        <a href="<?php echo str_replace('-150x150','',$image); ?>" class="lightbox" rel="gallery">
          <img src="<?php echo $image; ?>" alt="">
        </a>
      </div>
    </div>
    <?php
  }
}
?>

Photos are displayed as they should, but it is also required to display the description of the photos specified in the admin panel under each photo in the gallery. For some reason, there is very little documentation on the functions of the gallery in WP, a lot has to be done at random, an attempt to get into the core did not bring any results, because it is difficult.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
Igor Vorotnev, 2014-02-26
@TARAKANhoy

1. Why use `has_shortcode();`, it's possible in another way...
2. Slightly modified suggestion by @ZetRider :

if ( get_post_gallery() ) :
    $gallery = get_post_gallery( get_the_ID(), false );
// array of ids;
    $ids = explode(',', $gallery['ids']);
// array of urls;
    $images = $gallery['src'];
    $i = 0;
    foreach( $images as $image ) {
      $title = get_the_title($ids[$i]);
// тут свой формат вывода
      echo '<img src="'.$image.'"><span>'.$title.'</span><br>';
      $i++;
    }
  endif;

But, the `get_the_title();` function only gives us 1, and not the best field. Having an array of IDs (and these are the IDs of attachments), you can use one of the functions of attachments - more data will be returned there. For example, you can use `wp_get_attachment_image()`, `wp get attachment metadata()`.

T
Timur Kalimullin, 2014-02-24
@ZetRider

It is possible like this:

if( has_shortcode( $post->post_content, 'gallery' ) ) {
    $gallery 		= get_post_gallery($post->ID, false);
    $gallery_ids 	= explode(',', $gallery['ids']);
    $g_i = 0;
    foreach( $gallery['src'] as $image ) {
        $title_img = get_the_title($gallery_ids[$g_i]);
        ?>
        <div class="gallery-box-img">
            <div class="gallery-img">
                <a href="<?php echo str_replace('-150x150','',$image); ?>" class="lightbox" rel="gallery">
                    <img src="<?php echo $image; ?>" alt="<?php echo $title_img; ?>">
                </a>
            </div>
        </div>
        <?php
    $g_i++;
    }
}

This is of course a solution, but more like a crutch.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question