How to wrap 2 functions into one call

Ok, so I’m working on a WordPress project (PHP) using Advanced Custom Fields and a little bit of PHP. I’ve created 2 functions that will create a container div around some text:

<section>

 <?php container_start(); ?>

   Text goes here

 <?php container_end(); ?>

</section>

This generates the following code:

<section>

 <div class="container">

  Text goes here

 </div>

</section>

This is great as it works as expected. What is going behind the scenes are these 2 functions:

function container_start() {

$container = get_sub_field('container'); 

if ($container): 
echo '<div class="container">';
endif; 

}

function container_end() {

$container = get_sub_field('container'); 

if ($container): 
echo '</div>';
endif; 

}

Question is: Is there a way to optimize how is this accomplished? I find it not too practical having to call 2 functions just to add and then close a div. Is there a way I could wrap this into just one call?

Read more here: How to wrap 2 functions into one call

Leave a Reply

Your email address will not be published. Required fields are marked *