functions - How to automatically add custom classes to headings in content area

admin2025-01-07  4

Is there any way I can automatically add a custom class to heading tags (like h1, h2, h3, etc..,) in the content area. I know that I can manually add a class to a sub-heading from the post editor, but I want the class to be applied to all the headings in all the posts.

for instance, a normal heading will look something like this <h2>sub-heading</h2>. I want this to be changed to <h2 class="custom-class">sub-heading</h2>.

Is there any way I can automatically add a custom class to heading tags (like h1, h2, h3, etc..,) in the content area. I know that I can manually add a class to a sub-heading from the post editor, but I want the class to be applied to all the headings in all the posts.

for instance, a normal heading will look something like this <h2>sub-heading</h2>. I want this to be changed to <h2 class="custom-class">sub-heading</h2>.

Share Improve this question asked Jan 4, 2017 at 6:44 vamsivamsi 312 silver badges4 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 0

Yes, there are a couple of ways you can do this. One is by creating a filter in the functions.php file, like what is done here: https://stackoverflow.com/questions/10468697/how-to-add-a-class-to-a-html-element-using-filters-in-wordpress

The other would be to use Javascript/jQuery to add classes to all the headers post load. Using the php variant would be more elegant, though.

转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1736262446a819.html

最新回复(0)