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 trialPavle Lucic
10,801 PointsWhy if (! dynamic_sidebar('uptop')) ?
I dont understand, this line of code.
It tells if uptop no exist , initialize this line of code?
Basicaly inside if (false) , do something?
4 Answers
Sue Dough
35,800 PointsIt is saying if the sidebar uptop does not exist. Then you could put code after it to do something if it does not exist. So yes you are correct.
Rich Bagley
25,869 PointsHi Pavle,
I've removed my answer and replaced it with this as it was obviously causing confusion.
This code can be used as a single line, as per your example. In your video at the 2:15 mark Zac explains how this works (and is what I was trying to explain - but obviously not very well :D)
You can also use this as a code block which contains alternate code should the widget area not contain anything. Zac goes over that in this video at the 6:10 mark.
-Rich
geoffrey
28,736 PointsRich Bagley thank you for pointing this video. Pavle Lucic I get you, I find as well this piece of code a bit misleading at first, for some reasons.
Firstly, the name of the function itself is misleading, when you see dynamic_sidebar() you might think that's something used only for sidebar elements... I guess that was named this way by wordpress developers because most widgets go inside the sidebar ? Not sure...
Lastly, that would have been more obvious for beginners to give more details, maybe by showing an example such as the one at the bottom.
<?php if ( ! dynamic_sidebar( 'uptop' ) ): ?> //if the uptop widget doesn't exist, this check the existence of the uptop Id I guess.
<h1>You should install a widget</h1> <!-- then display display this message, otherwise, display the installed widgets-->
<?php endif; ?>
Pavle Lucic
10,801 PointsHey Rich Bagley , yes the Zak in first video said that dynamic_sidebar check** if dynamic sidebar exist*, and on second video he said *if dynamic sidebar not exist*
geoffrey Yes it is little confusing , even now.
In wordpress codex the definition of dynamic_sidebar() is This function calls each of the active widget callbacks in order, which prints the markup for the sidebar. If you have more than one sidebar, you should give this function the name or number of the sidebar you want to print. **This function returns true on success and false on failure.**
By this logic, calling dynamint_sidebar( ! ) -> will return false, And the there will be no call for active widgets?
So by this logic dynamic_sidebar( ! "widgetname") shoult return false, and not call any widget?
I done little code in js to test that logic (it is handy beacuse dev console)
// create function that always returns TRUE -> put inside variable
var b = function returnTrue () {
return true;
}
// Testing if function -> IF b == true , it will alert that is true
if (b) {
alert("IT IS TRUE");
}
// Testing -> if b != true , it will alert false.
if (!b) {
alert("IT IS TRUE");
}
else alert("FALSE");