MathCiet MathCiet - 1 month ago 8
PHP Question

Wordpress Custom Field - shorter code

Is it possible to write shorter code then?
I'm starting with php/wordpress...

<?php while( have_rows('members') ):
the_row();
// vars
$image = get_sub_field('member_picture');
$member = get_sub_field('member');
$link = get_sub_field('member_link');
$link2 =ltrim($link, "http://");
$memberabout = get_sub_field('member_about')
?>
<div class="members">
<div class="member"><?php echo $member; ?></div>
<div class="memberpicture"><img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt'] ?>" /></div>
<div class="memberabout"><?php echo $memberabout; ?></div>
<div class="memberlink"><a href="<?php echo $link; ?>"><?php echo $link2; ?></a></div>
</div>
<?php endwhile; ?>

Answer

In order to shorten the code, you might try it like this :

<?php while( have_rows('members') ): the_row(); 
     $image = get_sub_field('member_picture');
?>
    <div class="members">
        <div class="member"><?php echo get_sub_field('member'); ?></div>
        <div class="memberpicture"><img src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt'] ?>" /></div>
        <div class="memberabout"><?php echo get_sub_field('member_about'); ?></div>
        <div class="memberlink"><a href="<?php echo get_sub_field('member_link'); ?>"><?php echo ltrim($link, "http://"); ?></a></div>
    </div>
<?php endwhile; ?>