Refactoring code for the ACF Custom Page Builder

In my last post, I walked through how to build a simple but powerful page builder field group in ACF Pro and provided PHP to check for the content type and then include the proper PHP file to output it.

I want to refactor that code now to make it more robust in far fewer lines. When looking at the original code:

It becomes pretty clear that this is overly verbose and rather amateurish in its approach. To be consistent, I used values in the Content Type select that match the partial PHP file names except for the Slider/Carousel component. By updating that partial’s file name to be template-parts/content-blocks/_content_block_slider_carousel.php, the code below will work. Instead of having this big bloated complex if statement, it could be simplified to the following:

Now, it’s getting the correct partial file based on the corresponding value of the Content Type select field, reducing the amount of PHP and making the information we’ve already entered into the site do more work for us.

I’m all about using what we already have as efficiently as possible. Note that the checks for it being a slider are still necessary due to the modified code output required, but otherwise no checks for content type need to be done to output the correct HTML.

Contact Me

Have a question for me? Get in touch with me here and I'll respond as quickly as I can!
  • This field is for validation purposes and should be left unchanged.
Loading...