【发布时间】:2013-12-26 16:52:08
【问题描述】:
我想用 Bootstrap 3 制作一个响应式主题。但是,我需要自动将 CSS 类 .img-responsive 添加到每个帖子图片中,因为我需要图片是响应式的。
请建议我在 WordPress 的 functions.php 文件或任何其他允许我自动添加 CSS 类的文件中添加什么。
【问题讨论】:
标签: css wordpress twitter-bootstrap-3 wordpress-theming
我想用 Bootstrap 3 制作一个响应式主题。但是,我需要自动将 CSS 类 .img-responsive 添加到每个帖子图片中,因为我需要图片是响应式的。
请建议我在 WordPress 的 functions.php 文件或任何其他允许我自动添加 CSS 类的文件中添加什么。
【问题讨论】:
标签: css wordpress twitter-bootstrap-3 wordpress-theming
我有以下代码,并且由于帖子来自以前的版本......他们没有工作......
我该怎么办?
<figure class="image"><img class="lazyload p402_hide" alt=""
width="800" height="400" data-sizes="auto" data-
src="https://br.clubnation.club/wp-content/uploads/2020/01/primeira-
loja-oficial-de-harry-potter-sera-aberta-em-nova-york-2.jpg" data-
srcset="https://br.clubnation.club/wp-
content/uploads/2020/01/primeira-loja-oficial-de-harry-potter-sera-aberta-
em-nova-york-2.jpg 328w, https://br.clubnation.club/wp-
content/uploads/2020/01/primeira-loja-oficial-de-harry-potter-sera-
aberta-em-nova-york-3.jpg 380w, https://br.clubnation.club/wp-
content/uploads/2020/01/primeira-loja-oficial-de-harry-potter-sera-
aberta-em-nova-york-4.jpg 528w, https://br.clubnation.club/wp-
content/uploads/2020/01/primeira-loja-oficial-de-harry-potter-sera-
aberta-em-nova-york-5.jpg 704w" />
<figcaption>A loja será inaugurada no próximo verão.
(Fonte: Warner Bros/Divulgação)</figcaption></figure>
【讨论】:
因为你需要为你所有的帖子图片添加它,那么你需要为内容添加一个钩子并添加
function add_responsive_class($content){
$content = mb_convert_encoding($content, 'HTML-ENTITIES', "UTF-8");
$document = new DOMDocument();
libxml_use_internal_errors(true);
$document->loadHTML(utf8_decode($content));
$imgs = $document->getElementsByTagName('img');
foreach ($imgs as $img) {
$img->setAttribute('class','img-responsive');
}
$html = $document->saveHTML();
return $html;
}
现在将钩子添加到内容中
add_filter ('the_content', 'add_responsive_class');
但是,如果您已经有 img 的类并且需要添加一个新类,那么您可以参考PHP equivalent to jQuery addClass。或者,您可以简单地这样做:
$existing_class = $img->getAttribute('class');
$img->setAttribute('class', "img-responsive $existing_class");
上面的代码有效.. 我用它来删除 src 和 data-src 以进行图像延迟加载。希望对你有用
【讨论】:
the_content() 函数使用<html> 和<body> 标签包装帖子内容。
您可以让所有图片在 CSS 中响应,如下所述:
I want to apply css class(bootstrap) .img-responsive on all content images
使用 LESS,但 Sass 版本几乎相同:
img {
@include img-responsive();
}
【讨论】:
//all classes i need in a string
$classes = 'img-responsive attachment-post-thumbnail size-post-thumbnail wp-post-image';
//then i use my variable
the_post_thumbnail('post_thumbnail', array( 'class' => $classes ));
【讨论】:
这种方法更好:https://wordpress.stackexchange.com/questions/108831/add-css-class-to-every-image
function add_image_class($class){
$class .= ' additional-class';
return $class;
}
add_filter('get_image_tag_class','add_image_class');
唯一需要注意的是,当您插入新图像时,它会在编辑窗格中添加类,并且不会影响现有图像。
【讨论】:
我认为最简单的方法是像这样使用 CSS。
.content img { height: auto; max-width: 100%; }
.content 是包含您的帖子内容的区域。
注意:您可能还想像这样覆盖 .wp-caption 类。
.wp-caption { width: auto !important; }
【讨论】:
div 中,并向其中添加一个名为 content 的类或任何您想要的名称。
不太确定这个答案在性能方面有多好,但它确实有效。只需将其放在functions.php中即可。
function img_responsive($content){
return str_replace('<img class="','<img class="img-responsive ',$content);
}
add_filter('the_content','img_responsive');
请注意,class="img-responsive 后面需要空格,以免与其他类合并。
【讨论】:
我有同样的问题,将这个函数添加到 functions.php 对我有用。
function add_image_responsive_class($content) {
global $post;
$pattern ="/<img(.*?)class=\"(.*?)\"(.*?)>/i";
$replacement = '<img$1class="$2 img-responsive"$3>';
$content = preg_replace($pattern, $replacement, $content);
return $content;
}
add_filter('the_content', 'add_image_responsive_class');
【讨论】:
我认为您不需要添加类来使图像响应。 只需从特色图像中删除高度宽度,图像肯定会变得响应。
在你的function.php中有代码删除高度宽度
add_filter( 'post_thumbnail_html', 'remove_thumbnail_dimensions', 10, 3 );
function remove_thumbnail_dimensions( $html, $post_id, $post_image_id ) {
$html = preg_replace( '/(width|height)=\"\d*\"\s/', "", $html );
return $html;
}
【讨论】:
您可以在主题的 header.php 文件中使用 jquery 代码。
jQuery(function() {
jQuery(img).addClass('img-responsive');
});
【讨论】:
当您在循环中显示帖子时,您可以这样做:
the_post_thumbnail('thumbnail', array('class' => 'img-responsive'));
更多详情请见https://codex.wordpress.org/Function_Reference/the_post_thumbnail。
【讨论】:
类不会在上传时添加,而是在图像发送到编辑器时添加。您可以使用image_send_to_editor 过滤器添加一个或多个类。 This example 添加了一个 fancybox 类。
【讨论】: