Every custom field created in the last lesson is just sitting in the database until a template actually reads it back out. Two functions do that, and the choice between them comes down to one question: do you need to use the value, or just print it?
<?php the_field( 'client_name' ); ?>
<?php $client = get_field( 'client_name' ); ?>| Function | Use it when |
|---|---|
the_field() | You just want the value echoed straight into the page, right where you call it — the field-level equivalent of the_title() |
get_field() | You need the value in a variable first — to check it, format it, or pass it to another function |
In practice, get_field() covers nearly everything, since even simple output usually wants an if check first (below). the_field() is really only for the simplest, always-present case.
A field's type decides the shape of the value get_field() hands back — this is the part that trips people up first:
| Field Type | Return value |
|---|---|
| Text / Textarea / Number | A plain string or number — safe to echo directly (after escaping) |
| True / False | A plain PHP boolean — use directly in an if |
| Image | An array by default — ['url'], ['alt'], ['width'], ['height'], ['ID'], among others |
| Link | An array — ['url'], ['title'], ['target'] |
<?php $client = get_field( 'client_name' ); ?>
<?php if ( $client ) : ?>
<p>Client: <?php echo esc_html( $client ); ?></p>
<?php endif; ?>
<?php $photo = get_field( 'featured_photo' ); ?>
<?php if ( $photo ) : ?>
<img src="<?php echo esc_url( $photo['url'] ); ?>" alt="<?php echo esc_attr( $photo['alt'] ); ?>">
<?php endif; ?>Fields are optional — always check before using
Custom fields are almost never required — an editor can always leave one blank. Every read should be checked before it's used, the same way $client and $photo are checked above. Skipping the check doesn't crash the page outright, but it does produce empty <img> tags and stray labels with nothing after them on any post where the field was left blank.
With a way to both write and read structured data, the next lesson covers the field type built specifically for repeating structured data — a gallery of images, a list of features, anything that isn't just one value.