wordpress - img_caption_shortcode 的实现没有效果
问题描述
我在孩子的functions.php中添加了以下代码
add_filter( 'img_caption_shortcode', 'my_img_caption_shortcode', 10, 3 );
function my_img_caption_shortcode( $output, $attr, $content ) {
$attr = shortcode_atts( array(
'id' => '',
'align' => 'alignnone',
'width' => '',
'caption' => ''
), $attr );
if ( 1 > (int) $attr['width'] || empty( $attr['caption'] ) ) {
return '';
}
if ( $attr['id'] ) {
$attr['id'] = 'id="' . esc_attr( $attr['id'] ) . '" ';
}
return '<div ' . $attr['id']
. 'class="wp-caption ' . esc_attr( $attr['align'] ) . '" '
. 'style="max-width: ' . ( 10 + (int) $attr['width'] ) . 'px;">'
. do_shortcode( $content )
. '<p class="wp-caption-text">TEST TEST' . $attr['caption'] . '</p>'
. '</div>';
}
尽管如此,the_content 中的所有图像都会在帖子中输出,如下所示:
<figure class="wp-block-image size-large"><img loading="lazy" width="960" height="637" src="https://www.example.com/img.jpg" alt="example" class="wp-image-9987" srcset=" (...) "><figcaption>My caption text</figcaption></figure>
上面的钩子似乎没有任何效果。
使用 the_content() 在 single.php 中读取帖子的内容。
- 缓存被清除
- 最新的 WP 版本
- 没有错误信息
解决方案
尝试确保img_caption_shortcode()
正在调用该函数。在我们的例子中,几年前有人添加了自定义短代码处理程序,而自定义处理程序没有应用img_caption_shortcode
过滤器。
推荐阅读
- python - Python 脚本中的 KIBANA 搜索 API 不起作用?
- flutter - 边框仅在 Flutter TextField 的左上角
- delphi - 在delphi中读取文件(处理文件)
- amazon-web-services - 为网站寻找最佳服务器实例:一般问题
- javascript - 对象作为未定义返回
- javascript - 用数字替换字符串中的字母
- git-bash - 日期未使用在 git bash 上提供的正确时区
- c# - 将文本与大量 If/else 进行比较
- sql - 数组类型的列不从函数 PostgreSQL 返回
- r - 如何使用 R 生成这样的 Plot?应该使用什么库?