Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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!