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

PHP

PHP If/Else Question

I am working on a WordPress project that gives the user the option of including sections or not... currently, if the user opts NOT to include a section, it creates a gap where the content should be. Is there a way to re-structure the PHP so that if that section is not included, the other sections just move around it?

Pasting the current PHP below.

<?php if (get_option('cap_social_links') != "false"){ ?>
        <section id="socialize"> 
            <div class="lines-top"><div class="lines-bottom"> 
                <h2 class="hide">Socialize</h2> 
                <ul> 
                    <?php if (get_option('cap_twitter_username') != ""){if (get_option('cap_twitter_username') != "Your Twitter Username"){?>
                    <li class="tweets"><a href="http://twitter.com/<?php echo get_option('cap_twitter_username'); ?>" target="_blank">Tweets About Our<br /> Upcoming Events</a></li> <?php } } ?>
                    <?php if (get_option('cap_facebook_url') != ""){if (get_option('cap_facebook_url') != "Your Twitter Username"){?>
                    <li class="likes"><a href="<?php echo get_option('cap_facebook_url'); ?>" target="_blank">Like us on<br /> Facebook</a></li> <?php } } ?>
                    <?php if (get_option('cap_feedburner') != ""){if (get_option('cap_feedburner') != "Your Feedburner URL"){?>
                    <li class="subscribe"><a href="<?php echo get_option('cap_feedburner'); ?>" target="_blank">Subscribe To Our<br /> Email Updates</a></li> 
                    <?php  } } ?>
                </ul> 
            </div></div> 
        </section> 
 <?php } ?>

Thanks for your help!

3 Answers

It sounds like you might have some javascript/jquery acting on your page elements (or even injecting new ones) - one way to track those down is to figure out what classes are showing up that don't seem to be coming from php or html, then search the theme files for those (or you could search the page for script tags and see what's being called). Soon you'll be building your own themes instead of trying to clean up other peoples'!

I hope so! Thanks for your help!

probably ' != "false" ' is not enough of a test - if the user had enabled that at one point then changed their mind, they would have an empty option field, so it wouldn't be false (it would be true but empty)

change that to something like if (get_option('cap_social_links') && get_option('cap_social_links') ! = "") {}

(or just get the option once and save it in a variable, then test it)

Hm. I couldn't get that to work either.

I actually tried to just comment the whole section out since I knew the user didn't need it... but the ugly gap remained. :(

Do I need an else statement to hide it if it it's not displayed, rather than just not displaying it?

Did you comment out the html as well as the php? If you just get rid of your if/else you'll still get the section, divs and empty ul, so that's probably what's creating the gap. If you just delete it all does it fix it?

I did. I commented out the whole section that I pasted in my first post. But I'm suspicious that something else was causing the issue... I admit I tried to "hack" it by just moving the divs around so the 'gapping' div would be at the bottom and less noticeable... but when I did that the gap appeared in an entirely different place (seemingly) unrelated to the div that I thought the gap was holding a space for (the gap was normally at the top right of four divs in the space the social links should go, but when I moved the social links to the bottom right, assuming the gap would be in the bottom right, the gap appeared in the middle left - just below the top left and above the new bottom left).

As a quick solution to the issue, I actually just did a "display: none" on the offending section and a negative margin to fill the gap. I just couldn't figure out how it was being created... there was no positional styling on these section elements at all... and yet this gaping, squirmy hole. I'm probably just missing something in the PHP... I'm not very comfortable with it yet.

This was a premium theme, but they refused to offer any support to 'modify' it... which was sort of frustrating because you would think a well-built theme wouldn't leave a gaping hole in the template for a section they chose to make optional to the user. :P