Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

WordPress

How to apply classes to wysiwyg field content?

i've one slight problem. I've built a news custom post type with custom post type ui and advanced custom fields.in the backend i normally use text area fields since they are unstyled without any enclosing tags so they are easier to place. but in the case of the wysiwyg field that way isn't viable since per default content is enclosed in paragraph tags.

<div>
    <?php the_field( 'news_post' ); ?>
</div>

leads to

<div>
    <p> content </p>
</div>

but is there a way to apply a class to the p tag like

<div>
    <p class="mydesiredclass"> content </p>
</div>

best regards ralf

4 Answers

István Turupoli
István Turupoli
11,950 Points

If the class is only for styling, you could just add the class to the parent div, and use a child selector ( div > p ) in the css.

Crisoforo Gaspar Hernández
Crisoforo Gaspar Hernández
18,302 Points

You could use the php function: strip_tags

as follows:

$text = "<p>Test paragraph.</p><a href='#fragment'>Other text</a>";
echo strip_tags($text);
echo "\n";

// Allows <p> and <a>
echo strip_tags($text, '<p><a>');

Outputs

Test paragraph. Other text
<p>Test paragraph.</p> <a href="#fragment">Other text</a>
Matt Campbell
Matt Campbell
9,767 Points

Wrap <?php the_content(); ?> in a div and give the div the class. Then you can .class element and you're golden.

thanks! to style based on the wrapping parent div is a good idea. easy to apply. i think i will go for that. thanks again!