Skip to content

Instantly share code, notes, and snippets.

@robneu
Last active August 29, 2015 14:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save robneu/11204704 to your computer and use it in GitHub Desktop.
Save robneu/11204704 to your computer and use it in GitHub Desktop.
Check an array to see if any of the values aren't empty. Also includes some example usage functions and an example template to show how the function could be used to display some Advanced Custom Fields data.
<?php
/**
* Check an array to see if any values aren't empty.
*
* @param $array the array of data that you want to check for filled values.
* @return bool true if the array has at least one non-empty value.
* @author Robert Neu <http://wpbacon.com>
*/
function prefix_array_has_data( $array ) {
if ( count( array_filter( (array) $array ) ) !== 0 ) {
return true;
}
return false;
}
<?php
/**
* A template part for displaying some ACF stuff.
*
* @see prefix_get_the_acf_stuff()
*/
?>
<section class="some-acf-stuff">
<ul class="acf-list">
<?php if ( $value_one ) { ?>
<li class="value-one acf">
<a href="<?php echo esc_url( $value_one ); ?>"><?php _e( 'Value One', 'text-domain' ); ?></a>
</li>
<?php } ?>
<?php if ( $value_two ) { ?>
<li class="value-two acf">
<a href="<?php echo esc_url( $value_two ); ?>"><?php _e( 'Value Two', 'text-domain' ); ?></a>
</li>
<?php } ?>
<?php if ( $value_three ) { ?>
<li class="value-three acf">
<a href="<?php echo esc_url( $value_three ); ?>"><?php _e( 'Value Three', 'text-domain' ); ?></a>
</li>
<?php } ?>
<?php if ( $value_four ) { ?>
<li class="value-four acf">
<a href="<?php echo esc_url( $value_four ); ?>"><?php _e( 'Value Four', 'text-domain' ); ?></a>
</li>
<?php } ?>
</ul><!-- .acf-list -->
</section><!-- .some-acf-stuff -->
<?php
/**
* Grab some ACF values and return them.
*
* @author Robert Neu
* @uses Advanced Custom Fields
*/
function prefix_get_the_data() {
if ( ! class_exists( 'acf' ) ) {
return;
}
$data = array(
'field_one' => get_field( 'field_one' ),
'field_two' => get_field( 'field_two' ),
'field_three' => get_field( 'field_three' ),
'field_four' => get_field( 'field_four' ),
);
return $data;
}
/**
* Set up a template for some ACF values after checking if they exist.
*
* @author Robert Neu
* @uses Advanced Custom Fields
* @return $output templated ACF content.
*/
function prefix_get_the_acf_stuff( $fields = array() ) {
if ( ! class_exists( 'acf' ) ) {
return;
}
// Do nothing if we have no content.
if ( ! prefix_array_has_data( $fields ) ) {
return;
}
// Declare each array value as a variable.
extract( $fields, EXTR_SKIP );
// Display some ACF data with a template.
ob_start();
include_once( locate_template( 'templates/partial/example-template.php' ) );
$output = ob_get_clean();
return $output;
}
/**
* Echo the output of an ACF template.
*
* @author Robert Neu
* @uses Advanced Custom Fields
* @see prefix_get_the_data()
* @see prefix_get_the_acf_stuff()
*/
function prefix_the_acf_stuff() {
echo prefix_get_the_acf_stuff( prefix_get_the_data() );
}
// Add the ACF stuff to a Genesis action hook.
add_action( 'genesis_before_content', 'prefix_the_acf_stuff' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment