在 the_content 内显示图片说明

Show image captions inside the_content

我使用 the_content() 阅读了我自己的 Wordpress 主题中的 post 内容。如何实现内容中的图像带有字幕输出?最好没有插件,直接通过循环或 functions.php.

(媒体已提供标题等)

 <?php
       $beforeEachImage = "<div>";
       $afterEachImage = "</div>";
       preg_match_all("/(<img [^>]*>)/",get_the_content(),$matches,PREG_PATTERN_ORDER);
       for( $i=0; isset($matches[1]) && $i < count($matches[1]); $i++ ) {
             echo $beforeEachImage . $matches[1][$i] . $afterEachImage;
       }
    ?>

使用此代码获取图像。

post/page 内容中带有标题的图像会自动输出为 figure 元素,包括标题,请在此处查看官方文档:https://developer.wordpress.org/themes/functionality/media/images/#caption