Wordpress 如何将图像包装在_content()中的div中;?
然后你将媒体添加到wordpress主题的帖子中,比如图片, 它总是这样:Wordpress 如何将图像包装在_content()中的div中;?,wordpress,Wordpress,然后你将媒体添加到wordpress主题的帖子中,比如图片, 它总是这样: <img src="http://localhost:8888/example/wp-content/uploads/example.jpg" alt="leaf graphic" title="leaf graphic" class="alignright size-medium wp-image-3109" height="25" width="30"/> 但我的目标是将我添加到帖子中的任何图像包装
<img src="http://localhost:8888/example/wp-content/uploads/example.jpg" alt="leaf graphic" title="leaf graphic" class="alignright size-medium wp-image-3109" height="25" width="30"/>
但我的目标是将我添加到帖子中的任何图像包装到div中,如下所示:
<div class="card">
<img class="img-fluid" src="http://localhost:8888/example/wp-content/uploads/example.jpg" alt="" />
</div>
有什么方法可以做到这一点吗?您可以使用“媒体发送到编辑器”过滤器。
当您将任何媒体插入编辑器时,它将通过该过滤器
add_filter('media_send_to_editor', 'inserted_image_div', 10, 3 );
function inserted_image_div( $html, $send_id, $attachment )
{
return '<div class="card">'.$html.'</div>';
}
add_filter('media_send_to_editor','inserted_image_div',10,3);
插入的函数\u image\u div($html、$send\u id、$attachment)
{
返回“”。$html。“;
}
如果您试图用标题包装图像,则此操作无效,因为WordPress在将图像和标题输出到屏幕时使用了一个快捷码。在这种情况下,它似乎完全忽略了媒体发送到编辑器的过滤器
要用标题包装图像,需要编辑短代码的输出
add_filter( 'img_caption_shortcode', 'my_img_caption_shortcode', 10, 3 );
function my_img_caption_shortcode( $empty, $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;">'
. '<div class="CLASS-NAME-HERE">' . do_shortcode( $content ) . '</div>'
. '<p class="wp-caption-text">' . $attr['caption'] . '</p>'
. '</div>';
}
看
'<div class="CLASS-NAME-HERE">' . do_shortcode( $content ) . '</div>'