【问题标题】:Show image captions inside the_content在 the_content 中显示图片标题
【发布时间】:2023-03-20 22:53:01
【问题描述】:

我使用 the_content() 在我自己的 Wordpress 主题中阅读帖子内容。如何实现内容中的图像与标题一起输出?最好没有插件,直接通过循环或functions.php。

(媒体已提供标题等)

【问题讨论】:

  • 您是否只需要内容中的图像?
  • 是的,目前。我能够为特色图片解决它,但不能解决 the_content。

标签: wordpress wordpress-theming


【解决方案1】:

帖子/页面内容中带有标题的图像会自动输出为figure元素,包括标题,请参阅此处的官方文档:https://developer.wordpress.org/themes/functionality/media/images/#caption

【讨论】:

  • 您的提示使我找到了问题的原因。我填写的是“标题”而不是“标题”。是否可以输出标题而不是标题?
  • AFAIK 不,至少没有插件或类似的钩子。
【解决方案2】:
 <?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;
       }
    ?>

使用此代码获取图像。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-22
    • 1970-01-01
    • 1970-01-01
    • 2016-10-09
    • 1970-01-01
    • 2012-01-04
    • 1970-01-01
    相关资源
    最近更新 更多