简体   繁体   中英

How to filter Wordpress gallery captions?

I would like to filter my wordpress galleries caption text. When an image caption is empty, I would like to add the post title + $i. The output code should be:

<dl>
  <dt>
    <a><img src="http://example.com"></a>
  </dt>
  <dd>
    POST TITLE 1
  </dd>
</dl>

I have been looking for a wp_get_attachment_metadata filter but I couldn't find a solution.

Thank you.

The Gallery Image Captions (GIC) plugin adds a much-needed hook to the WordPress media.php file. Using GIC as an example, you can write a filter to handle empty image captions.

// This will display a default caption for attachments with empty captions.
function my_default_gallery_image_caption($attachment_id, $captiontag, $selector, $itemtag) {

    $id = $attachment_id;

    $my_image_meta = galimgcaps_get_image_meta($id);

    $caption_string = ( !empty( $my_image_meta['caption'] ) ) ? $my_image_meta['caption'] : 'YOUR CUSTOM CAPTION GOES HERE';

     return "<{$captiontag} class='wp-caption-text gallery-caption' id='{$selector}-{$id}'>" . $caption_string . "</{$captiontag}></{$itemtag}>";
}
add_filter('galimgcaps_gallery_image_caption', 'my_default_gallery_image_caption', 10, 4);

Screen Grab

在此处输入图片说明

I hope that helps. Holler if you have any questions :-)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM