問題描述

This webpage 包含 Wordpress 插入的影像。用於插入第一個影像的程式碼是:


    <a href="http://steven.doig.com.au/files/2013/06/Forest_Legacy_m.jpg">
        <img class="size-large wp-image-887" alt="a Forest Legacy group" src="http://steven.doig.com.au/files/2013/06/Forest_Legacy_m-1024x681.jpg" width="1024" height="681" />
    </a> a Forest Legacy group

此圖片由 CSS 控制:

#content .wp-caption a img {
    width: 614px;
    height: auto;
}

我想使這個影像響應。我已經插入了 CSS:

@media (max-width:988px) {
    #content .wp-caption a img {
        width: 99.03225806%; /* 614/620 */
        height: auto;
    }
}

但是,DIV.wp-caption 仍然保持在 604px,如 Wordpress 文章中所指定的。我已經嘗試將其指定為百分比 (97.41935483%),但 Wordpress 將其解釋為 104px 。

內聯 CSS 覆蓋了我插入到樣式表中的 CSS 。

<div id="attachment_887" class="wp-caption alignnone" style="width: 614px">

任何想法如何使.wp-caption 響應?

最佳解決方案

你將要使用:

@media (max-width: 988px){
  .wp-caption {
    /* Force the box to be 100% */
    width: 100% !important;
  }
  #content .wp-caption a img {
    /* Scale down if too big */
    max-width: 99.03225806%; /* 614/620 */
    height: auto;
  }
}

次佳解決方案

另一種可能性是改變短碼輸出,使寬度不再是 hard-coded 。修改食典示例沒有寬度:

add_filter('img_caption_shortcode', 'my_img_caption_shortcode_filter',10,3);

/**
 * Filter to replace the  shortcode text with HTML5 compliant code
 *
 * @return text HTML content describing embedded figure
 **/
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>';
}

http://codex.wordpress.org/Function_Reference/add_filter#Example

參考文獻

注:本文內容整合自 Google/Baidu/Bing 輔助翻譯的英文資料結果。如果您對結果不滿意,可以加入我們改善翻譯效果:薇曉朵技術論壇。