使Wordpress图片标题具有响应性


10

该网页包含Wordpress插入的图像。用于插入第一个图像的代码是:

[caption id="attachment_887" align="alignnone" width="604"]
    <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[/caption]

此图像由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标题保持在604px,如在Wordpress帖子中指定的那样。我尝试将其指定为百分比(97.41935483%),但Wordpress将其重新解释为104px。

内联CSS覆盖了我插入样式表中的CSS。

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

关于如何使.wp-caption响应的任何想法?

Answers:


11

您将要使用:

@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;
  }
}

7

另一种可能性是更改短码输出,以便不再对宽度进行硬编码。修改Codex示例以使其不具有宽度:

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

/**
 * Filter to replace the [caption] 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


2

这是一个更简单,更干净的解决方案:

function my_img_caption_shortcode_width($width, $atts, $content)
{
    return 0;
}

add_filter('img_caption_shortcode_width', 'my_img_caption_shortcode_width', 10, 3);
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.