Advanced Custom Fields

BP Design LLC

Here’s a link to one of my favorte and most helpful plugins:
Advanced Custom Fields.

http://www.advancedcustomfields.com/
It just flat out makes customizing a wordpress site so easy it’s ridiculious, especially with all the add-ons available.
Below is some of the code to use when implementing the fields you’ve added.

Display a field

 
Retrieving a field as a variable

$variable = get_field('field_name');
// do something with $variable
?>

Using conditional statements

get_field returns false if (value == “” || value == null || value == false)
if(get_field('field_name'))
{
echo '

' . get_field('field_name') . '

';
}
?>

Working with Array values
checkbox, select, relationship, repeater

$values = get_field('field_name');
if($values)
{
echo '

      ';

 

      foreach($values as $value)

 

      {

 

      echo '

    • ' . $value . '

';
}
echo '

';
}

// always good to see exactly what you are working with
var_dump($values);
?>


Working with Images – URL

Working with Images – ID

By using the ID, you can retrieve any crop size of the image and even get the name of the file!


<?php echo get_the_title(get_field('image_test')) ?>

Working with the Repeater Field

repeater can be accessed by get_field or the_repeater_field / the_sub_field

    • sub_field_1 = , sub_field_2 = , etc

$sub_field_3 = get_sub_field(‘sub_field_3’);
// do something with $sub_field_3
?>

 

Randomly select a repeater field row

$rows = get_field('repeater_field_name');
$row_count = count($rows);
$i = rand(0, $row_count - 1);
echo $rows[ $i ]['sub_field_name'];
?>

Getting vales from another page

$other_page = 12;
?>

 

// get variable
$variable = get_field(‘field_name’, $other_page);
// repeater field: note that the_sub_field and get_sub_field don’t need a post id parameter
if( have_rows(‘repeater_field_name’, $other_page) ): ?>

    • sub_field_1 = , sub_field_2 = , etc

$sub_field_3 = get_sub_field(‘sub_field_3’);
// do something with $sub_field_3
?>

Query posts with ACF values

Example of finding Events (post type) where location (custom field – select) equals Melbourne (value).
Lots to read here: http://codex.wordpress.org/Template_Tags/get_posts.

$posts = get_posts(array(
'numberposts' => -1,
'post_type' => 'event',
'meta_key' => 'location',
'meta_value' => 'melbourne'
));
if($posts)
{
echo '

      ‘;

 

      foreach($posts as $post)

 

      {

 

‘;
}
echo ‘

‘;
}
?>