Create a custom function to do simple work

Question

Now I make a function for each post meta field I need, check if has value, add html wrapper and translate or add text to it instead of making this in the template file.

Example: I put this code in my class as gallery() method:

<?php 
$images = get_field('gallery');
$size = 'full'; // (thumbnail, medium, large, full or custom size)
if( $images ): ?>
    <ul>
        <?php foreach( $images as $image_id ): ?>
            <li>
                <?php echo wp_get_attachment_image( $image_id, $size ); ?>
            </li>
        <?php endforeach; ?>
    </ul>
<?php endif; ?>

So I can use it as MyClass::gallery();

Maybe I create another method to just check whether the field returns true or false.

Isn’t this the preferred method in terms of performance? and What is your advice to make similar theme programming easier?

0
techno 4 months 0 Answers 12 views 0

Leave an answer