I want to make a responsive theme with Bootstrap 3. However, I need to automatically add the CSS class .img-responsive
to every post image because I need the images to be responsive.
Please suggest me what I need to add in WordPress’s functions.php
file or any other file that will allow me to add the CSS class automatically.
since you need to have it for all of your post images, then you need to add a hook for the content and add
now add the hook to the content
However, if you already have classes for the img and you need to add a new class then you can refer to PHP equivalent to jQuery addClass. Or, you can simply do this:
The code above works .. i use it to remove src and data-src for image lazy loading. Hope it works for you
This approach is better: https://wordpress.stackexchange.com/questions/108831/add-css-class-to-every-image
Only caveat is that it adds the class within the edit pane when you insert new images and doesn’t affect pre existing ones.
I think the easiest way is to use CSS like this.
Where .content is the area that contains your post content.
Note: You may also want to override the .wp-caption class as well like so.
I had the same question, and adding this function to functions.php worked for me.
When you display post in your loop, you could do :
See https://codex.wordpress.org/Function_Reference/the_post_thumbnail for more details.
Not quite sure how good this answer is performance wise but it works. Just put this in functions.php.
Please note that you need the space after
class="img-responsive
so it doesn’t merge with other classes.You can use jquery code on the header.php file of your theme.
I think you don’t require to add class to make image responsive.
just remove height width from featured image, image will become responsive definitely.
There is code put in your function.php to remove height width
Classes are not added on upload, but when the image is sent to the editor. You can use the
image_send_to_editor
filter to add one or more classes. This example adds afancybox
class.You could just make all images responsive in the css as mentioned here:
I want to apply css class(bootstrap) .img-responsive on all content images
That uses LESS, but the Sass version is pretty much the same:
I have the following code, and as the posts are from previous versions … They did not work …
What should I do?