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 WordPress Theme Development Adding a Blog to a WordPress Theme Coding the Blog Homepage

If Statement

I am trying to use an if statement to display part of a custom post type, only if there is content in it.

It seems to work, but it removes my css styling, does anyone know why this is? Here is my code:

<?php if ( the_field('link') ) : ?>
        <a target="_blank" href="<?php the_field('link') ?>"><p class="register-green">Click to Attend - It's Free!</p></a>
<?php endif; ?>

Sorry it did not copy my code in properly, this is wrapped in the following php:

<?php if ( the_field('link') ) : ?> <?php endif; ?>
Sean T. Unwin
Sean T. Unwin
28,690 Points

Hi Kelly,

I have edited your post to display the code properly. Please see this thread regarding posting code to the forum. :)

Sean T. Unwin
Sean T. Unwin
28,690 Points

When you check the source, is the paragraph tag being written?

If so, are there other class names being written and the one you want is not?

1 Answer

Sean T. Unwin
Sean T. Unwin
28,690 Points

Try using get_field('link') in your if statement. Continue to use the_field('link') inside the href of the anchor.

Like so:

<?php if ( get_field('link') ) : ?>
        <a target="_blank" href="<?php the_field('link') ?>"><p class="register-green">Click to Attend - It's Free!</p></a>
<?php endif; ?>