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

Why isn't this working and is there a better way?

Im using a options panel in my Wordpress theme to output some CSS.

The option control is a on/off switch. I assume that means on = 0 and off = 1

That could be wrong as well.

Here is what I have in my php/css file

#logo img{  
<?php if($center-logo == 0): ?>
margin:auto;
<?php else: ?>
margin:none;
}
<?php endif ?>


// For reference, the code below is what is in the options panel
array(
  'id'       => 'center-logo',
  'type'     => 'switch', 
  'title'    => __('Center Logo', 'redux-framework-demo'),
  'subtitle' => __('Look, it\'s on!', 'redux-framework-demo'),
  'default'  => true,
),

Generally, binary selectors use 0 for false and 1 for true, so I would assume that 1 would be on and 0 would be off. Have you played around with these?

When set to 1 no CSS is applied. When set to 0 the margin:auto is applied no matter if the switch is on or off.

Ah okay, thanks for that. Good luck, Danny!

5 Answers

Have you tried:

#logo img {  
<?php if($center-logo): ?>
margin:auto;
<?php else: ?>
margin:none;
<?php endif ?>
}

That didnt seem to work either. :(

Did some research on their documentation, have you tried:

#logo img {  
<?php if($center-logo == TRUE): ?>
margin:auto;
<?php else: ?>
margin:none;
<?php endif ?>
}

Yeah. I have tried that. The only way I get any kind of movement from the logo is if I use the number 0.

Otherwise nothing happens at all.

It seems like maybe there is a problem with the ELSE statement? If I use the code you gave me and put in FLASE then the logo becomes centered.

I would assume 1 would mean On and 0 would mean off, have you tried using 1 instead of 0?

#logo img {  
<?php if($center-logo == 1): ?>
margin:auto;
<?php else: ?>
margin:none;
<?php endif ?>
}

If you have the center logo switch on, it seems that would equal 1, therefore margin: auto would be applied, else margin: none.

Thats what I thought too.

But when I turn the switch off, the logo is still centered.

Have you tried echoing out the value of that variable? From their documentation:

global $redux_demo;

echo 'Switch value: ' . $redux_demo['opt-switch'];

So you would use

global $redux_demo;

echo 'Switch value: ' . $redux_demo['center-logo'];

The only thing that may be different is the $redux_demo global. According to their documentation:

"Be sure to change $redux_demo to the value you specified in your opt_name argument."

Yeah, I don't really see why I would need to do that. Doesnt make sense.

To see what the value is so you can ensure your if/else is looking for the appropriate value to compare.

Still struggling with this. Any help would be greatly appreciated.

Did you try what I suggested above? Echoing out the variable to determine its value?