我将WordPress与Twitter Bootstrap 3结合使用,当涉及到带有标题的图像时,我会遇到一个问题。
带有字幕的图像具有高度和宽度属性,这会阻止它们做出响应。
我尝试了以下代码,但这只适用于没有标题的图像:
function change_uploaded_image_html( $html ) {
// Removes height and width attribute from images when they
// are uploaded.
$classes = \'img-responsive\';
if ( preg_match( \'/<img.*? class=".*?" \\/>/\', $html ) ) {
$html = preg_replace( \'/(<img.*? class=".*?)(".*?\\/>)/\', \'$1 \' . $classes . \'$2\', $html );
}
else {
$html = preg_replace( \'/(<img.*?)\\/>/\', \'$1 class="\' . $classes . \'" \\/>\', $html );
}
$html = preg_replace( \'/(height|width)="\\d*"\\s/\', "", $html );
return $html;
}
然后,我尝试了本论坛另一篇帖子中的另一种方法,该方法将宽度属性设置为nothing,但我没有成功,因为图像上的宽度和高度属性仍然存在:
function my_img_caption_shortcode_filter( $val, $attr, $content=null ){
extract( shortcode_atts( array(
\'id\' => \'\',
\'align\' => \'\',
\'width\' => \'\',
\'caption\' => \'\'
), $attr ) );
if ( 1 > ( int ) $width || empty( $caption ) )
return $val;
$capid = \'\';
if ( $id ) {
$id = esc_attr( $id );
$capid = \'id="figcaption_\'. $id . \'" \';
$id = \'id="\' . $id . \'" aria-labelledby="figcaption_\' . $id . \'" \';
}
return \'<figure \' . $id . \'class="wp-caption \' . esc_attr($align) . \'" >\'
. do_shortcode( $content ) . \'<figcaption \' . $capid
. \'class="wp-caption-text">\' . $caption . \'</figcaption></figure>\';
}
add_filter( \'img_caption_shortcode\', \'my_img_caption_shortcode_filter\',10 ,3 );
我正在寻找一种用PHP和WP的内置过滤器来实现这一点的方法,我想我已经接近了答案,但不知道该朝哪个方向发展。
非常感谢nav
SO网友:Friss
还有一种可能性,当您在编辑器中选择要添加到内容的图像时,它会更改图像标记
add_filter( \'image_send_to_editor\', \'remove_img_attribute\', 10, 8 );
function remove_img_attribute( $html, $id, $caption, $title, $align, $url, $size, $alt )
{
$imagetpl = \'<figure>
<a href="%s" title="%s"><img src="%s" alt="%s" class="img-responsive %s" /></a>
%s
</figure>\';
$figcaption = (!empty($caption)) ? sprintf(\'<figcaption>%s</figcaption>\',$caption):null;
$image = wp_get_attachment_image_src($id,\'large\');
return sprintf($imagetpl,$image[0],$title,$image[0],$alt,$align,$figcaption);
}
SO网友:HanSoloShotFirst
我使用了下面的解决方案。它被发现了here
add_shortcode( \'wp_caption\', \'fixed_img_caption_shortcode\' );
add_shortcode( \'caption\', \'fixed_img_caption_shortcode\' );
function fixed_img_caption_shortcode($attr, $content = null) {
if ( ! isset( $attr[\'caption\'] ) ) {
if ( preg_match( \'#((?:<a [^>]+>\\s*)?<img [^>]+>(?:\\s*</a>)?)(.*)#is\', $content, $matches ) ) {
$content = $matches[1];
$attr[\'caption\'] = trim( $matches[2] );
}
}
$output = apply_filters( \'img_caption_shortcode\', \'\', $attr, $content );
if ( $output != \'\' )
return $output;
extract( shortcode_atts(array(
\'id\' => \'\',
\'align\' => \'alignnone\',
\'width\' => \'\',
\'caption\' => \'\'
), $attr));
if ( 1 > (int) $width || empty($caption) )
return $content;
if ( $id ) $id = \'id="\' . esc_attr($id) . \'" \';
return \'<div \' . $id . \'class="wp-caption \' . esc_attr($align) . \'" >\'
. do_shortcode( $content ) . \'<p class="wp-caption-text">\' . $caption . \'</p></div>\';
}
SO网友:Digamber
THA.T\'s A. LoT oF CoDE, 我F YoU WA.NT THE 我MA.GE oR我G我NA.L s我ZE A.ND NoT THE CRoPPED 五、ERs我oN WHY DoN\'T YoU UsE WP_GET_A.TTA.CHMENT_我MA.GE_sRC()
我NsTEA.D?
十、A.;十、A.;
我N REPLY To YoUR CoMMENT:->
十、A.;十、A.;
十、A.;A.DD_F我LTER( \'我MG_CA.PT我oN_sHoRTCoDE\', \'MY_我MG_CA.PT我oN_sHoRTCoDE\', 1.0, 3. );十、A.;FUNCT我oN MY_我MG_CA.PT我oN_sHoRTCoDE( $EMPTY, $A.TTR, $CoNTENT ){十、A.; $A.TTR = sHoRTCoDE_A.TTs( A.RRA.Y(十、A.; \'我D\' => \'\',十、A.; \'A.L我GN\' => \'A.L我GNNoNE\',十、A.; \'W我DTH\' => \'\',十、A.; \'CA.PT我oN\' => \'\'十、A.; ), $A.TTR );十、A.; 我F ( $A.TTR[\'我D\'] ) {十、A.; $A.TTR[\'我D\'] = \'我D=“”\' . EsC_A.TTR( $A.TTR[\'我D\'] ) . \'“” \';十、A.; }十、A.; $CLA.ssEs = \'我MG-REsPoNs我五、E\';十、A.;十、A.; 我F ( PREG_MA.TCH( \'//\', $CoNTENT) ) {十、A.;十、A.; $CoNTENT= PREG_REPLA.CE( \'/()/\', \'$1. \' . $CLA.ssEs . \'$2.\', $CoNTENT);十、A.; }十、A.; ELsE {十、A.; $CoNTENT= PREG_REPLA.CE( \'/(/\', \'$1. CLA.ss=“”\' . $CLA.ssEs . \'“” \\/>\', $CoNTENT);十、A.; }十、A.;
十、A.;十、A.;$CoNTENT= PREG_REPLA.CE( \'/(HE我GHT|W我DTH)=“”\\D*“”\\s/\', “”“”, $CoNTENT);十、A.; RETURN \'\'十、A.; . Do_sHoRTCoDE( $CoNTENT ) . \'\' . $CA.PT我oN . \'\';}十、A.;
十、A.;
十、A.;十、A.;
TH我s WoRKED FoR ME, 我T 我T DoEsN\'T FoR YoU, YoU MA.Y WA.NT To LooK A.T oTHER HooKs THA.T A.RE FoRC我NG THE W我DTH oN YoUR 我MA.GEs
十、A.;