Skip to content

Instantly share code, notes, and snippets.

@astockwell
Last active August 29, 2015 14:06
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 astockwell/fdf8d67ed69e37b18dc7 to your computer and use it in GitHub Desktop.
Save astockwell/fdf8d67ed69e37b18dc7 to your computer and use it in GitHub Desktop.
Advanced Custom Fields Pristine Field Uses
<!-- Text Field -->
<?php $field_name_slug = get_field("field_name_slug"); if ( !empty($field_name_slug) ): ?>
<?php echo $field_name_slug; ?>
<?php endif; ?>
<!-- Image Field -->
<?php $image_field_name = get_field("image_field_name"); if ( $image_field_name ): ?>
<img src="<?php echo $image_field_name["url"]; ?>" alt="<?php echo $image_field_name["title"]; ?>" />
<?php endif; ?>
<!-- Gallery Field -->
<?php $gallery_field_name = get_field("gallery_field_name"); if( $gallery_field_name ): ?>
<div id="slider" class="flexslider">
<ul class="slides">
<?php foreach( $gallery_field_name as $image ): ?>
<li>
<img src="<?php echo $image["url"]; ?>" alt="<?php echo $image["title"]; ?>">
<p class="caption"><?php echo $image["caption"]; ?></p>
</li>
<?php endforeach; ?>
</ul>
</div>
<?php endif; ?>
<!-- Repeater Field -->
<?php $repeater_field_name = get_field("repeater_field_name"); if ( $repeater_field_name ): ?>
<?php foreach($repeater_field_name as $repeater_field_name_row): ?>
<!-- Inner loop html here -->
<?php // DEFINE THE SUBFIELDS: ?>
<?php $sub_field_1 = $repeater_field_name_row['sub_field_1']; ?>
<?php $sub_field_2 = $repeater_field_name_row['sub_field_2']; ?>
<?php // ... more fields ... ?>
<?php // USE THE SUBFIELDS: ?>
<?php if ( !empty($sub_field_1) ): ?>
<p><?php echo $sub_field_1; ?></p>
<?php endif; ?>
<?php if ( !empty($sub_field_2) ): ?>
<p><?php echo $sub_field_2; ?></p>
<?php endif; ?>
<?php endforeach; ?>
<?php endif; ?>
<!-- Flexible Content Field -->
<?php if( have_rows('flexible_content_field_name') ): ?>
<ul>
<?php while ( have_rows('flexible_content_field_name') ): the_row(); ?>
<?php if( get_row_layout() == 'first_row_layout_name' ): ?>
<li>
<!-- Text Sub Field: -->
<?php $sub_field_name = get_sub_field('sub_field_name'); if ( !empty($sub_field_name) ): ?>
<?php echo $field_name_slug; ?>
<?php endif; ?>
<!-- Image Sub Field: -->
<?php $image_sub_field = get_sub_field("image_sub_field"); if ( $image_sub_field ): ?>
<img src="<?php echo $image_sub_field["url"]; ?>" alt="<?php echo $image_sub_field["title"]; ?>" />
<?php endif; ?>
</li>
<?php elseif( get_row_layout() == 'another_row_layout_name' ): ?>
<!-- More html/fields here -->
<?php endif; ?>
<?php endwhile; ?>
</ul>
<?php else: ?>
<p>No layouts found</p>
<?php endif; ?>
<!-- Relationship Field -->
<?php $relationship_field = get_field('relationship_field'); if( $relationship_field ): ?>
<ul>
<?php foreach( $relationship_field as $related ): // variable must NOT be called $post (IMPORTANT) ?>
<li>
<a href="<?php echo get_permalink( $related->ID ); ?>">
<?php echo get_the_title( $related->ID ); ?>
</a>
<!-- Custom field from $related: -->
<?php $field_name = get_field("field_name", $related->ID); if ( !empty($field_name) ): ?>
<?php echo $field_name; ?>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
<!-- Reverse Relationship Lookup -->
<?php
$related_posts = get_posts(array(
'post_type' => 'book',
'meta_query' => array(
array(
'key' => 'isbn_number', // name of custom field
'value' => '"' . get_the_ID() . '"', // matches exactly "123", not just 123. This prevents a match for "1234"
'compare' => 'LIKE'
)
)
));
?>
<?php if( $related_posts ): ?>
<div class="related-books">
<h2>Books</h2>
<ul>
<?php foreach( $related_posts as $related ): ?>
<li>
<a href="<?php echo get_permalink( $related->ID ); ?>">
<?php echo get_the_title( $related->ID ); ?>
</a>
<!-- Custom field from $related: -->
<?php $field_name = get_field("field_name", $related->ID); if ( !empty($field_name) ): ?>
<?php echo $field_name; ?>
<?php endif; ?>
</li>
<?php endforeach; ?>
</ul>
</div>
<?php endif; ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment