I want to add a class to the p and h tags in the body in WordPress.

Asked 2 years ago, Updated 2 years ago, 129 views

WordPress

<?php the_content();?>

I would like to add a class to the p,h2,h3 output in .

<p class="my-class-name">Aiueo</p> 

I want to do something like that.

I found that I would add a different class to the body element, but I don't know how to add it to the body.Please let me know.

body_class()|Function|WordPress Developer Resources

wordpress

2022-09-30 21:40

1 Answers

Instead of the_content(); you should use get_the_content(); to retrieve the contents of the body and edit them to the desired design.
Here is a simple example using str_replace

:
$content_string=get_the_content();

$content_string = str_replace('<p','<p class="classname"', $content_string);
$content_string = str_replace('<h2', '<h2class="classname"', $content_string);
$content_string = str_replace('<h3', '<h3class="classname"', $content_string);

echo$content_string;


2022-09-30 21:40

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.