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 How to Make a Website with WordPress WordPress Widgets and Custom Menus How to Create Widgetized Areas in WordPress

Christie Parkhurst
Christie Parkhurst
6,059 Points

Calling custom widgetized area in header.php - what does this code mean?

This line that calls the widgetized area shown in the Wordpress track "how to create widgetized areas in WordPress":

    <?php if ( ! dynamic_sidebar( 'uptop' ) );
 ?>

How does this work? Does that mean if there is no dynamic_sidebar, then it would show the widget?

4 Answers

Simon Woodard
Simon Woodard
6,545 Points

Well 'dynamic_sidebar()' is a function, so you should find the inner workings of the function at another place in the script, or on another php script that is linked

Christie Parkhurst
Christie Parkhurst
6,059 Points

Thank you!! That is what I was wondering about.

Simon Woodard
Simon Woodard
6,545 Points

There doesn't necessarily need to be an else statement. If it finds that 'uptop' isn't there it just won't do anything else and continue on with the rest of the code

Christie Parkhurst
Christie Parkhurst
6,059 Points

Thanks for your time. I need to clarify something....I understand what will happen if it DOESN'T find the uptop, but if it DOES, where does it tell it to display it? Is that something that is just tied in to the function, behind the scenes?

Simon Woodard
Simon Woodard
6,545 Points

The code is checking to see if the dynamic_sidebar named 'uptop' is there or not, if it finds that it isn't there then it will run the following code

Christie Parkhurst
Christie Parkhurst
6,059 Points

Thanks for your help. I'm a newbie to this, so please bear with me, as the answer is probably obvious, but I'm just not getting it. I understand the code to mean, if uptop doesn't exist, then it will show the rest of the code that follows. But what if it finds 'uptop' IS there? Shouldn't there be an else statement that says what to do if it DOES exist? As it is, it works, it shows the uptop div & the rest of header code underneath it, but I just don't get how.

Chris Tattum
Chris Tattum
17,201 Points

Yes, I'm with Christie on this one - I don't understand the need for the if statement at all.

So I edited the code, and replaced it with the following:-

<?php dynamic_sidebar('uptop'); ?>

and it works exactly the same as before.

Looking at the WordPress codex, it says that the return value for dynamic_sidebar is true or false depending on whether the call to dynamic_sidebar itself was successful. This can be then used to display additional static content, if the widget is present. This isn't required in this case, so we're not concerned with the return value.

I will just say though, that I'm just starting out in WordPress development here so I may have missed something!