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

Shaun Taylor
Shaun Taylor
2,654 Points

Hide a button when field left blank...

I would like to hide a button when someone leaves a field blank in Wordpress...

I have this website which allows admins to post 'courses' on the 'book' page - http://seedcreativehub.co.uk/courses/free-social-media-marketing-training-and-work-experience/

You can see there, one of the courses someone has added. At the bottom, there is an event bright button, the admin is supposed to add a link to their event bright page but for the times there is no event bright page url entered - I would like to hide the button...

I have made these courses as custom post types. and the bit of code used to display this single course is as follows:

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

<?php the_content(); ?>
<p><a class="event-button" href="<?php the_field( 'link' ); ?>" target="blank"> <img src="http://seedcreativehub.co.uk/wp-content/uploads/2014/08/custombutton.png"> </a></p>

<?php endwhile; else: ?>      

<div class="page-header">
<h1>Oh no!</h1>
</div>

<p>No content is appearing for this page!</p>

<?php endif; ?>

As you can see, this code pulls in the content and also the button and adds the link from the link field to the button with this bit: href="<?php the_field( 'link' ); ?>"

So I need this whole button to disappear if someone doesn't enter a url in the link field.

Any help would be massively appreciated!!

3 Answers

Andrew Shook
Andrew Shook
31,709 Points

All you need to do is wrap the html for the button in a php conditional statement. In this case, the if statement needs to check to see if the field is populated. It looks like you are using the Advance Custom Fields plugin so you can return the value of a field using get_field('field_name').

<?php if ( have_posts() ) : while ( have_posts() ) : the_post(); ?>

<?php the_content(); ?>
<?php if ( get_field('link') ):?>
        <p>
               <a class="event-button" href="<?php the_field( 'link' ); ?>" target="blank">
                          <img src="http://seedcreativehub.co.uk/wp-content/uploads/2014/08/custombutton.png">
               </a>
       </p>
<?php endif; ?>

<?php endwhile; else: ?>      

<div class="page-header">
<h1>Oh no!</h1>
</div>

<p>No content is appearing for this page!</p>

<?php endif; ?>
Shaun Taylor
Shaun Taylor
2,654 Points

Ah that's brilliant! thank you, I thought it might be something like that but i wasn't sure not he code and its difficult to search for if you're not sure on the correct terms 'conditional statement' etc...

I kind of jumped into Wordpress before learning PHP, as I needed to sell websites, and have tried to pick it up along the way... I'm planning to dedicate some time to PHP 101 soon hopefully to grasp a better understanding!

Thanks again and thanks for the quick reply :)

Kevin Korte
Kevin Korte
28,148 Points

Yep Andrew got it. That was going to be my suggestion.