Support

Account

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

Solved

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

    • rpk

    • January 19, 2015 at 3:15 am

    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

    • Dalton

    • January 19, 2015 at 6:07 am

    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');?>
    </div>

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

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

    Does that help?

    • rpk

    • January 19, 2015 at 9:49 am

    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.

    • rpk

    • January 19, 2015 at 10:34 am

    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.

We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.