Skip to content

Instantly share code, notes, and snippets.

@sophiawzey
Last active June 2, 2020 18:42
Show Gist options
  • Save sophiawzey/b9a83da4b0449f4c8e8400e3e83cabec to your computer and use it in GitHub Desktop.
Save sophiawzey/b9a83da4b0449f4c8e8400e3e83cabec to your computer and use it in GitHub Desktop.
[ACF PHP] using brackets #acf
<?php $count = count( get_field( 'field-name' ) ); ?> // can be used for counting rows in a repeater. works well w/ slick slider if you only want a slider on > 2 counts
$image = get_field('video_poster');
$size = 'medium';
if ( ! empty( $image ) ) {
// thumbnail
$thumb = $image['sizes'][ $size ];
} else {
$image = get_field('placeholder_image','option');
$thumb = $image['sizes'][ $size ];
}
?>
<div class="lozad" data-background-image="<?= $thumb ?>" style="background:url(/wp-content/themes/topfloor-parcel/assets/images/placeholder.png) no-repeat center; background-size: cover;"></div>
<?php $logo = get_field('header_logo', 'option'); ?>
<?php echo file_get_contents( $logo ); ?>
<?php
//use the ARRAY so that we can limit image size for the URL and reduce page size
$image = get_field( 'image' );
if ( $image ) {
$size = 'large';
$alt = $image['alt'];
$thumb = $image['sizes'][ $size ];
$width = $image['sizes'][ $size . '-width' ];
$height = $image['sizes'][ $size . '-height' ];
?>
<img class="lozad"
src="/wp-content/themes/topfloor-parcel/assets/images/placeholder.png"
data-src="<?= $thumb ?>"
alt="<?= $alt ?>"
width="<?= $width ?>"
height="<?= $height ?>"/>
<?php
}
<?php
// for a SINGLE post object
$post_object = get_field('post_object');
if( $post_object ){
// override $post
$post = $post_object;
setup_postdata( $post );
?>
<div>
<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
<span>Post Object Custom Field: <?php the_field('field_name'); ?></span>
</div>
<?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<?php }; ?>
$post_objects = get_field('post_objects');
if( $post_objects ){ ?>
<ul>
<?php foreach( $post_objects as $post){ // variable must be called $post (IMPORTANT) ?>
<?php setup_postdata($post); ?>
<li>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<span>Post Object Custom Field: <?php the_field('field_name'); ?></span>
</li>
<?php } ?>
</ul>
<?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<?php };
<?php $color = get_field('color'); ?>
<?= $color ?>
<?php
$posts = get_field('relationship_field_name');
if( $posts ){ ?>
<ul>
<?php foreach( $posts as $post){ // variable must be called $post (IMPORTANT) ?>
<?php setup_postdata($post); ?>
<li>
<a href="<?php the_permalink(); ?>"><?php the_title(); ?></a>
<span>Custom field from $post: <?php the_field('author'); ?></span>
</li>
<?php }; ?>
</ul>
<?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<?php }; ?>
<?php
// this is a repeater with a single post object
if ( have_rows( 'select_teasers' ) ) { ?>
<?php while ( have_rows( 'select_teasers' ) ) {
the_row();
$post_object = get_sub_field( 'page' ); // IMPORTANT - needs to be sub field
if ( $post_object ) {
// override $post
$post = $post_object;
setup_postdata( $post );
?>
<div>
<h3><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h3>
<span>Post Object Custom Field: <?php the_field( 'field_name' ); ?></span>
</div>
<?php wp_reset_postdata(); // IMPORTANT - reset the $post object so the rest of the page works correctly ?>
<?php } ?>
<?php } ?>
<?php } ?>
<?php
// an example of using ONE repeater and loopng through twice, first time for tab tables, second time for tab content.
if ( have_rows( 'tabs' ) ) { ?>
<section class="tabs-container">
<div class="grid-container-small">
<header>
<h2 class="title"><?= $title ?></h2>
</header>
<div class="tabs" data-tabs id="example-tabs">
<?php
$row = 1;
while ( have_rows( 'tabs' ) ) {
the_row();
$title = get_sub_field( 'title' );
?>
<li class="tabs-title <?php if ( $row == 1 ) {
echo 'is-active';
} ?>"><a href="#panel<?= $row ?>" aria-selected="true"><?= $title ?></a></li>
<?php
$row ++;
} ?>
</div>
<div class="tabs-content" data-tabs-content="example-tabs">
<?php
$row = 1;
while ( have_rows( 'tabs' ) ) {
the_row();
$left = get_sub_field( 'display_left' );
?>
<div class="tabs-panel <?php if ( $row == 1 ) {
echo 'is-active';
} ?>" id="panel<?= $row ?>">
<?= $left ?> testing content
</div>
<?php
$row ++;
} ?>
</div>
</div>
</section>
<?php } ?>
<?php
if ( have_rows( 'repeater' ) ) { ?>
<?php while ( have_rows( 'repeater' ) ) {
the_row();
$title = get_sub_field( 'title' );
?>
<!-- content here -->
<?php } ?>
<?php } ?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment