Advanced Custom Fields Snippets

Supplemental 734 views
0 Flares 0 Flares ×

The following snippets are for the plugin: Advanced Custom Fields.

Display a Field

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

If a Field has a Value {}

<?php if(get_field('field_name')) { ?>

<?php } ?>

Display a Repeater Field

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

While Has Repeater Field {}

<?php while(has_sub_field('')) { ?>

<?php } ?>

The above snippets of code have allowed me to customize and create a great variety of editable pages for clients. Below I show you how I used some of these snippets to create part of a sponsor attendee page. Some of the information provided by the sponsors was more detailed that others. Only certain sponsors provided phone numbers & emails for their attendees so I needed a way to show these contingent on whether they exist or not. Hope this helps!

Use Case

<div class="sponsor-post">
	<?php the_content(); ?>
	<div class="clear"></div>
	<a class="button1" href="<?php the_field('link'); ?>" target="_blank">Visit <?php the_title(); ?> Website</a>
	<?php if(get_field('attendees')) { ?>
		<h2><span aria-hidden="true" data-icon=""></span> <em>Attendees</em> from <?php the_title(); ?></h2>
	<?php } ?>
		<div class="speaker2">
			<?php while(has_sub_field('attendees')) { ?>
				<div class="speaker-200">
					<a href="<?php the_sub_field('link'); ?>"><img src="<?php the_sub_field('image'); ?>" alt="<?php the_title(); ?>"></a>
					<a href="<?php the_sub_field('link'); ?>"><?php the_sub_field('name'); ?></a>
					<?php if(get_sub_field('title')) { ?>
						<em><?php the_sub_field('title'); ?></em><br>
					<?php } ?>
					<?php if(get_sub_field('location')) { ?>
						<span aria-hidden="true" data-icon=""></span><?php the_sub_field('location'); ?><br>
					<?php } ?>
					<?php if(get_sub_field('phone')) { ?>
						<?php the_sub_field('phone'); ?><br>
					<?php } ?>
					<?php if(get_sub_field('email')) { ?>
						<a href="mailto:<?php the_sub_field('email'); ?>"><?php the_sub_field('email'); ?></a>
					<?php } ?>
					<div class="clear"></div>
			<?php } ?>
			<div class="clear"></div>
	<div class="clear"></div>

For Reference:

Advanced Custom Fields
Advanced Custom Fields Integration

Tagged with:
Posted in Wordpress


One comment on “Advanced Custom Fields Snippets
  1. suascat says:

    Thanks, this is a great tip for beginner. Here I want to add my side view to your code usage above if you wouldn’t mind. To minimize the database access for each condition you check, it would be best that you put/assign each query in variable and apply condition.


    This helps optimizing the performance when you are working on big projects.

    Thanks, :)

Leave a Reply

Your email address will not be published. Required fields are marked *


You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

No Trackbacks.


Register | Lost your password?
0 Flares Twitter 0 Facebook 0 Google+ 0 0 Flares ×