Show Menu
Cheatography

Advanced Custom Fields Cheat Sheet Cheat Sheet by

Echo Field Value

<?php the_field('field_name'); ?>

Store the field in a Variable

<?php $variable_name = get_field('field_name'); ?>

Print Array with Prefor­matted Text

<pre>
  <?php print_r(get_field('field_name')); ?>
</pre>

If Statement

<?php if(get_field('field_name')) { ?>
   If this is true, display this text.
<?php } ?>

Loop Through Array

<?php
  $items = get_field('field_name');
  if($items) { ?>
     <ul>
         <?php foreach($items as $item) { ?>
             <li> <pre><?php print_r($item); ?></pre></li>
         <?php } ?>
     </ul>
<?php } ?>

Retrieve Value from Taxonomy Term

<?php the_field('field_name', '{term_name}_{term_id}'); ?>

Ex: <?php the_field('field_name','post_tag_4'); ?>

Retrieve Value from User

<?php the_field('field_name', 'user_{user_id}'); ?>

Ex: <?php the_field('field_name','user_1'); ?>

ACF Pro Pull Value from Options Page

<?php the_field('field_name','option'); ?>

ACF Pro Add Options Page Function

if( function_exists('acf_add_options_page') ) {
	acf_add_options_page();	
}
 

Working with Images (Returning an ID)

<?php $image = wp_get_attachment_image_src(get_field('image_test'), 'full'); ?>
<img src="<?php echo $image[0]; ?>" alt="<?php echo get_the_title(get_field('image_test')) ?>" />
"­Ful­l" can be any size associated with Wordpress Theme

Repeater Field

<?php if( have_rows('repeater_field_name') ): ?>
 
    <ul>
 
    <?php while( have_rows('repeater_field_name') ): the_row(); ?>
 
        <li>sub_field_1 = <?php the_sub_field('sub_field_1'); ?>, sub_field_2 = <?php the_sub_field('sub_field_2'); ?>, etc</li>
        
        <?php 
        
        $sub_field_3 = get_sub_field('sub_field_3'); 
        
        // do something with $sub_field_3
        
        ?>
        
    <?php endwhile; ?>
 
    </ul>
 
<?php endif; ?>

Hide Custom Fields Panel Function in Admin Menu

add_filter('acf/settings/show_admin', '__return_false');
 

Comments

No comments yet. Add yours below!

Add a Comment

Your Comment

Please enter your name.

    Please enter your email address

      Please enter your Comment.

          Related Cheat Sheets

          WordPress $wpdb Cheat Sheet