6

I'm sure this is simple, but I've searched Google for about 30 minutes with no luck. I'd like to know if it's possible to adjust the default class of paragraphs that are output by the_content in Wordpress.

I've read that you can add filters to the theme's functions.php file, but this particular use of WordPress isn't using a theme. Think of a static HTML site (5 pages) with a main content area on each page. Wordpress is literally only used to change the contents of those 5 content areas by the client editing any of the necessary 5 posts.

The problem is that the p tags that WordPress spits out don't have any classes applied to them. Can I change that in my query or anything? Ideally I'd be able to change the paragraph class that is output on each page individually.

Thoughts?

Thanks in advance for any help!

4 Answers 4

5

Wrap your content with a div, and apply a class to that:

<div class="content"> <? the_content(); ?> </div> 

And style it like so:

.content p { ... } /* Page-specific */ .page-id-1 .content p { ... } 
Sign up to request clarification or add additional context in comments.

Comments

1

The accepted answer didn't really answer the question for me, but I found this post which gave me what I needed—so my solution ended up being something like this:

<?php $content = get_content(); $content = str_replace('<p', '<p class="default-class"', $content); ?> <div class="wrapper"><?php echo $content ?></div> 

That did the trick!

1 Comment

You have an error in your code. 1) $content = get_content(); - $content = get_the_content();
0
function wph_add_class_for_p_tag($content) { $content = str_replace('<p>', '<p class="SomeClass">', $content); return $content; } add_filter('the_content', 'wph_add_class_for_p_tag', 9999); add_filter('the_excerpt', 'wph_add_class_for_p_tag', 9999); 

2 Comments

please describe a little bit about the code that you've written
This code adds any class to the <p> tag that "content or excerpt" have developer.wordpress.org/reference/functions/the_content
0

Simple method without effecting the whole function -

<p class="card-text"><?php echo get_the_content();?></p> 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.