Support

Account

Home Forums Add-ons Flexible Content Field Show Empty Fields

Solving

Show Empty Fields

  • 							<?php if( have_rows('attach_files_multiple') ): while ( have_rows('attach_files_multiple') ) : the_row(); ?>  
    
    							<?php if( get_row_layout() == 'neck' ) : ?>
    							<div class="media">
    								<div class="media-left media-middle">
    								<!-- Image Start -->
    									<?php $image = get_sub_field('preview_prop_neck');
    									if( !empty($image) ): ?>
    										<img class="media-object" width="100px" src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
    									<?php endif; ?>
    								<!-- Image End -->
    								</div>
    								
    								<div class="media-body">
    									<?php $file = get_sub_field('prop_file_neck'); ?>
    									<h4 class="media-heading">
    										Neck Piece 
    										<a href="<?php echo $file['url']; ?>" class="pull-right btn btn-primary"><i class="fa fa-download"></i> Download</a>
    									</h4>
    								</div>
    							</div>
    							<?php elseif ( !empty(get_row_layout('neck')) ) : ?>
    								<div class="overlay"><h3>Helmet - No File Found</h3></div>
    								<div class="media">
    								<div class="media-left media-middle">
    								<!-- Image Start -->
    									<img class="media-object" width="100px" src="<?php bloginfo('template_directory'); ?>/images/pdo.png">
    								<!-- Image End -->
    								</div>
    								<div class="media-body">
    									<h4 class="media-heading">
    										Neck Piece 
    										<a href="#" class="pull-right btn btn-primary"><i class="fa fa-download"></i> Download</a>
    									</h4>
    								</div>
    							</div>
    							<?php endif; ?>
    							<?php endwhile; ?> <!-- End while ( have_rows( ) -->
    							<?php endif; ?> <!-- End if( have_rows ) ?> -->

    For some reason this is looping the <?php elseif ( !empty(get_row_layout(‘neck’)) ) : ?> twice.

    What im trying to achieve is if the field is populated display the top half, else display a placement area (the <div class=”overlay”>…etc)

  • you work with flexible fields, they can be repeated multiple times and with a own order. normally it doesn’t make sense to add a non-existing layout (if it is even possible)
    what you do with your code is to display for each flexible layout that is not neck to display the alternate content.

    if you try to display the overlay when image or file is not filled out (but neck flexible field is chosen than you had to do the if/else loop inside neck)

    something like this

    <!-- Image Start -->
    	<?php $image = get_sub_field('preview_prop_neck');
    	if( !empty($image) ): ?>
    		<img class="media-object" width="100px" src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
    	<?php else: ?>
    		<img class="media-object" width="100px" src="<?php bloginfo('template_directory'); ?>/images/pdo.png">
    	<?php endif; ?>
    <!-- Image End -->
  • It doesnt work, all i want to do is make it so if the layout isnt active/selected to display a placement/empty module instead.

    Lets say ive added the layout “Head and Neck” to the form, the remaining 15 layouts are inactive. Only Head and Neck would display and it wouldnt matter about

    <!-- Image Start -->
    	<?php $image = get_sub_field('preview_prop_shoulders');
    	if( !empty($image) ): ?>
    		<img class="media-object" width="100px" src="<?php echo $image['url']; ?>" alt="<?php echo $image['alt']; ?>" />
    	<?php else: ?>
    		<img class="media-object" width="100px" src="<?php bloginfo('template_directory'); ?>/images/pdo.png">
    	<?php endif; ?>
    <!-- Image End -->

    because it still wouldnt display (for example) the shoulder placement.

    Is there a way to check if its been populated and if it hasnt, display a placement, thats all im trying to achieve…

  • just to understand:

    • you had a flexible field with 15 layouts.
    • at page/article/cp you show that flexible field.
    • you/user can now select one or more of this flexible fields.

    and all that you wish is, when you didnt select/add the neck: you dont want to show neck?

    that is what flexible field normally does. (add optional content-blocks, and show them only if needed. with that you can have required fields like a keyvisual a with title, but only when you add keyvisual-block)

    what it does not do is: to look if a layout is already choosen 1 or more times, and if not display a alternate content.
    if you really need that you need to set a variable inside the get_row_layout loop. also you had to put the values into a variable first. after all layout you check each variable and check if it is filled or not & set alternates

    try if you can adapt this code

    <?php
    //define variables 
    $head= '';
    $shoulder ='';
    $neck ='';
    //check for layouts and the values inside
    if( have_rows('attach_files_multiple') ): while ( have_rows('attach_files_multiple') ) : the_row(); 
    
    if( get_row_layout() == 'neck' ) : 
    $neck .= 'all about neck';
    endif;
    if( get_row_layout() == 'shoulder' ) : 
    $shoulder .= 'all about shoulder';
    endif;
    if( get_row_layout() == 'head' ) : 
    $head .= 'all about head';
    endif;
    
    endwhile; endif;
    
    //check for filled values and build layout or the alternates
    if ($head) {
    echo $head;
    } else {
    echo 'no head was chosen ';
    }
    if ($shoulder) {
    echo $shoulder;
    } else {
    echo 'no shoulder was chosen ';
    }
    if ($neck) {
    echo $neck;
    } else {
    echo 'no neck was chosen ';
    }
     ?>
    

    why do you work with flexible fields when you replace anyway everything not filled with a default value?

Viewing 4 posts - 1 through 4 (of 4 total)

The topic ‘Show Empty Fields’ is closed to new replies.