Create a custom function to do simple work


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:

$images = get_field('gallery');
$size = 'full'; // (thumbnail, medium, large, full or custom size)
if( $images ): ?>
        <?php foreach( $images as $image_id ): ?>
                <?php echo wp_get_attachment_image( $image_id, $size ); ?>
        <?php endforeach; ?>
<?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?

techno 4 months 0 Answers 12 views 0

Leave an answer