Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

WordPress

Maja B.
Maja B.
12,984 Points

if custom field exists

Please help me with this WordPress code.

What I need WordPress to do is to:

  • check weather image_full exists
  • if it exists I need image-full to be selected
  • if it does not exists I need image_thumbnail to be selected

But in reality what happens is that WordPress selects image_thumbnail in any case - so if image_full exists or not.

P.S. image_full and image_thumbnail are my custom fields.

<div class="grid-4">

    <?php if ( $image_full ): ?>

    <a href="<?php the_field( 'image_full' ); ?>" rel="lightbox">

    <?php else: ?>

    <a href="<?php the_field( 'image_thumbnail' ); ?>" rel="lightbox">

    <?php endif; ?>

        <img src="<?php the_field( 'image_thumbnail' ); ?>" alt="<?php the_title(); ?>">
    </a>
</div>

1 Answer

Andrew Shook
Andrew Shook
31,709 Points

You need to use the function get_field an not the_field. The_field echo the result while get_field returns the value.

<div class="grid-4">

    <?php if ( get_field($image_full )): ?>

    <a href="<?php the_field( 'image_full' ); ?>" rel="lightbox">

    <?php else: ?>

    <a href="<?php the_field( 'image_thumbnail' ); ?>" rel="lightbox">

    <?php endif; ?>

        <img src="<?php the_field( 'image_thumbnail' ); ?>" alt="<?php the_title(); ?>">
    </a>
</div>
Maja B.
Maja B.
12,984 Points

Thanks. I've added get_field to line 2 But it still selects image_thumbnail in any case - so if image_full exists or not. Do you have some other ideas to make this code work?

Andrew Shook
Andrew Shook
31,709 Points

Can a use input both an image_thumbnail and a full image? If so, does the image_thumbnail need to take precedence over the image_full? Also, is the image full different from the featured image?

Maja B.
Maja B.
12,984 Points
  1. A custom post portfolio can have both: image_full and image_thumbnail. image_thumbnail is required, image_full is not required.

  2. Yes, image_thumbnail should take precedence over image_full (but only if image_full does not exist)

  3. Yes, image_full is different than featured_image. But it is also true that I do not use featured_image.

The rationality behind it is the slider. Several image_thumbnails appear on the page (like a gallery grid). When you click on one image, you open a slider. The slider displays thumbnail_image (but in the slider those images appear bigger so you can see the details of the drawing). But certain projects require a different image in the slider - full_image.

Those projects are the reason why I need to say: if image_full exists display image_full in the slider; if image_full does not exists than go ahead and display image_thumbnail in the slider.

Hope it makes sense. Thanks for help!!

Maja B.
Maja B.
12,984 Points

ANDREW'S CODE WORKS FINE, JUST REPLACE $image_full WITH 'image_full'.

Like this:

<div class="grid-4">

    <?php if ( get_field( 'image_full' )): ?>

    <a href="<?php the_field( 'image_full' ); ?>" rel="lightbox">

    <?php else: ?>

    <a href="<?php the_field( 'image_thumbnail' ); ?>" rel="lightbox">

    <?php endif; ?>

        <img src="<?php the_field( 'image_thumbnail' ); ?>" alt="<?php the_title(); ?>">
    </a>
</div>