Home › Forums › General Issues › Getting an if else to work
Hi,
I would like to get the below code to show either “school-life-video” row of data with the html and sub fields etc or the “school-life-link” row. But i can’t seem to make the if or if else work.
Can anyone help out please?
<?php
// loop through the rows of data
if( have_rows('school_life_holder') ):
// add a counter
$count = 0;
$group = 0;
while ( have_rows('school_life_holder') ) : the_row();
if ($count % 3 == 0) {
$group++;
?>
<div id="<?php echo $group; ?>" class="row group-<?php echo $group; ?>" data-equalizer> <!--Begin Row:-->
<?php
}
?>
<?php
if( get_row_layout() == 'school-life-link' ):
?>
<div class="large-4 medium-4 columns panel" data-aos="fade-up" data-aos-delay="150">
<div><a href="<?php echo the_sub_field('link'); ?>" target="_blank"><img src="<?php echo the_sub_field("staff_pic") ?>" alt="<?php the_title(); ?>"></a></div>
<div class="profile-box"><a href="<?php echo the_sub_field('link'); ?>" target="_blank"><strong><?php echo the_sub_field('staff_title'); ?></strong></a></div>
</div>
<?php
elseif( get_row_layout() == 'school-life-video' ):
?>
<div class="video-parent">
<div><a href="<?php echo the_sub_field('link'); ?>" target="_blank"><img src="<?php echo the_sub_field("staff_pic") ?>" alt="<?php the_title(); ?>"></a></div>
<div class="video-inner">
<a href="<?php echo the_sub_field('link'); ?>" target="_blank"><img class="bounce_button" src="#"></a>
</div>
</div>
<div class="profile-box"><a href="<?php echo the_sub_field('link'); ?>" target="_blank"><strong><?php echo the_sub_field('staff_title'); ?></strong><br><?php echo the_sub_field('staff_job'); ?></a></div>
<?php
if ($count % 3 == 2) {
?>
</div>
<?php
}
$count++;
endwhile;
else :
// no rows found
endif; ?>
I have created this using “flexible Content” & Repeaters to choose between “link” or “video” option as each block is entered.
I think you might need to close off a endif; down the bottom somewhere.
personally I go with brackets for long if else statements that way I can see whats opened and closed easier
I’m also confused about a closing div div tag down the bottom in the if count % 3 == 2. does this need to be outside the the link if?
See if this helps
<?php
if(have_rows('school_life_holder')) {
// add a counter
$count = 0;
$group = 0;
while ( have_rows('school_life_holder')) {
the_row();
if ($count % 3 == 0) {
$group++; ?>
<div id="<?php echo $group; ?>" class="row group-<?php echo $group; ?>" data-equalizer> <!--Begin Row:-->
<?php
} // count
if( get_row_layout() == 'school-life-link' ) { ?>
<div class="large-4 medium-4 columns panel" data-aos="fade-up" data-aos-delay="150">
<div><a href="<?php echo the_sub_field('link'); ?>" target="_blank"><img src="<?php echo the_sub_field("staff_pic") ?>" alt="<?php the_title(); ?>"></a></div>
<div class="profile-box"><a href="<?php echo the_sub_field('link'); ?>" target="_blank"><strong><?php echo the_sub_field('staff_title'); ?></strong></a></div>
</div>
<?php
} // end get link
elseif( get_row_layout() == 'school-life-video' ) { ?>
<div class="video-parent">
<div><a href="<?php echo the_sub_field('link'); ?>" target="_blank"><img src="<?php echo the_sub_field("staff_pic") ?>" alt="<?php the_title(); ?>"></a></div>
<div class="video-inner">
<a href="<?php echo the_sub_field('link'); ?>" target="_blank"><img class="bounce_button" src="#"></a>
</div>
</div>
<div class="profile-box"><a href="<?php echo the_sub_field('link'); ?>" target="_blank"><strong><?php echo the_sub_field('staff_title'); ?></strong><br><?php echo the_sub_field('staff_job'); ?></a></div>
<?php
} //end get video
if ($count % 3 == 0) {
echo '</div>'; //----I've moved this outside the previous if statement. Is it suppose to close off the starting if div?.
}
} //end while
} //if have rows
?>
I Managed to workout out, I had renamed the label on the flexible content, which ACF pro dosnt like, i had to create a new flexible content area and then re populate it. And i included the missing column container missing.
The Last Endif was needed so thank you ichabod1799
‘
<?php
// loop through the rows of data
if( have_rows(‘school_box’) ):
// add a counter
$count = 0;
$group = 0;
while ( have_rows(‘school_box’) ) : the_row();
//print(get_row_layout());
if ($count % 3 == 0) {
$group++;
?>
<div id=”<?php echo $group; ?>” class=”row group-<?php echo $group; ?>” data-equalizer> <!–Begin Row:–>
<?php
}
?>
<?php
if( get_row_layout() == ‘link_box’ ):
?>
<div class=”large-4 medium-4 columns panel” data-aos=”fade-up” data-aos-delay=”150″>
<div>” target=”_blank”>” alt=”<?php the_title(); ?>”></div>
<div class=”profile-box”>” target=”_blank”><?php echo the_sub_field(‘staff_name’); ?></div>
</div>
<?php
elseif( get_row_layout() == ‘video_box’ ):
?>
<div class=”large-4 medium-4 columns panel” data-aos=”fade-up” data-aos-delay=”150″>
<div class=”video-parent”>
<div>” target=”_blank”>” alt=”<?php the_title(); ?>”></div>
<div class=”video-inner”>
” target=”_blank”>
</div>
</div>
<div class=”profile-box”>” target=”_blank”><?php echo the_sub_field(‘staff_name’); ?></div>
</div>
<?php
endif;
if ($count % 3 == 2) {
?>
</div>
<?php
}
$count++;
endwhile;
else :
// no rows found
endif; ?>
‘
You must be logged in to reply to this topic.
Welcome to the Advanced Custom Fields community forum.
Browse through ideas, snippets of code, questions and answers between fellow ACF users
Helping others is a great way to earn karma, gain badges and help ACF development!
The most recent ACF Chat Friday featured a live demo of how to register CPTs directly in the plugin, one of our most requested features. Check out the summary below for a replay of the demo, and don’t forget to register for the next session! https://t.co/k2KQ3WWBAz
— Advanced Custom Fields (@wp_acf) March 9, 2023
© 2023 Advanced Custom Fields.
We use cookies to offer you a better browsing experience, analyze site traffic and personalize content. Read about how we use cookies and how you can control them in our Cookie Policy. If you continue to use this site, you consent to our use of cookies.