Home Forums ACF PRO Add a class to the last paragraph tag of a wysiwyg field


Add a class to the last paragraph tag of a wysiwyg field

  • Is there a way to tell acf to add a certain class only to the last paragraph tag of a wysiwyg field? so that the user isn’t in need to assign it manually in tinymce (and i have to provide a class setting in the tinymce settings beforehand)? Is there a way to target the last paragraph specifically? Best regards Ralf

  • Rather than attempt this with ACF, I would do this with CSS. You can wrap the field in a div, like this:

    <div class="my-wysiwyg">
     <?php the_field('my_wysiwyg');?>

    Then just apply the styling to the last <p> tag with CSS:

    .my-wysiwyg p:last-child {
      font-size: 30px;

    Does that help?

  • thanks for your reply! i am aware of the :last-child way but i am a bit of reluctant adding a wrapping div just for that purpose to the html markup.

  • Ok i think i figured things out.

    function last_paragraph( $content ){
      if ( is_page( 222 ) ) {
        return preg_replace('/[\s\S]*\K(<p>)/', '<p class="mytrail">', $content, 1);
    add_filter('acf_the_content', 'last_paragraph');

    I’ve set the function only to a certain page and on that page it targets only acf fields ( which are in my case only text fields, text areas and wysiwyg fields). Text fields and text areas don’t have any wrapping p tags therefor only wysiwyg fields are targeted. And with that small function it is possible that the user is able to add more paragraphs in each wysiwyg field and still the class is set on the last paragraph of each wysiwyg field.

Viewing 4 posts - 1 through 4 (of 4 total)

You must be logged in to reply to this topic.