ThomasLarge
11/21/2017 - 4:07 PM

ACF Helpers

ACF Helpers

<?php if ( get_field( 'field_name' ) ): ?>

  This is displayed wen the field_name is TRUE or has a value.

<?php else: // field_name returned false ?>

  This is displayed when the field is FALSE, NULL or the field does not exist.

<?php endif; // end of if field_name logic ?>
# Include panels - acf pro
<?php
if( get_field('content_panels') ):

	while ( has_sub_field('content_panels') ) :

		if( get_row_layout() == 'columns_panel' ):

				 get_template_part( 'panels/columns' );

		elseif( get_row_layout() == 'two_col_image_left' ):

		    get_template_part( 'panels/two-col-left' );

	  endif;

	endwhile;

endif; ?>


# Add columns classes to the number of acf rows 
<?php if( have_rows('columns') ):
	// Vars
	$colIndex = 0;
	$col_HTML = '';

		while ( have_rows('columns') ) : the_row(); $colIndex++;
		
			$column_text = get_sub_field('content');
      // This is the HTML that is printed
			$col_HTML.= "<div class=\"[REPLACECOLCSS]\">$column_text</div>";

		endwhile;

	// pre define the allowed columns
	$allowed_classnames = array(
	1 => 'medium-8 medium-centered columns',
	2 => 'medium-4 medium-offset-2 columns',
	3 => 'medium-3 columns',
	);
	// Make sure you alway limit the amount of rows in ACF

	// set a default classname
	$classname_to_use = $allowed_classnames[1];

	// check if the $number_of_cols exist in the predefined classnames
	if ( array_key_exists( $colIndex , $allowed_classnames ) ) {
	// set the classname to be used
	$classname_to_use = $allowed_classnames[$colIndex];
	}

	echo str_replace( '[REPLACECOLCSS]', $classname_to_use, $col_HTML );

?>

<?php endif; ?>