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 trialKelly MacPhee
74 PointsIf 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; ?>
Sean T. Unwin
28,690 PointsHi Kelly,
I have edited your post to display the code properly. Please see this thread regarding posting code to the forum. :)
Sean T. Unwin
28,690 PointsWhen 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
28,690 PointsTry 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; ?>
Kelly MacPhee
74 PointsKelly MacPhee
74 PointsSorry it did not copy my code in properly, this is wrapped in the following php:
<?php if ( the_field('link') ) : ?> <?php endif; ?>