0

I'm writing a WP theme, and I want the main content area to change width based on whether there's an active sidebar or not. To make this easier, I'm using bootstrap.

The problem is that the output is blank.

Here's the code I'm trying to use to do the calculations:

<!-- Calculate content width based on sidebars -->
<?php
if ( is_active_sidebar( 'left_sidebar' ) && !is_active_sidebar( 'right_sidebar' ) ) {
 $mainspan = "9";
}
?>
<?php
if ( !is_active_sidebar( 'left_sidebar' ) && is_active_sidebar( 'right_sidebar' ) ) {
 $mainspan = "span9";
}
?>
<?php
if ( is_active_sidebar( 'left_sidebar' ) && is_active_sidebar( 'right_sidebar' ) ) {
 $mainspan = "span6";
}
?>
<?php
if ( !is_active_sidebar( 'left_sidebar' ) && !is_active_sidebar( 'right_sidebar' ) ) {
 $mainspan = "span12";
}
?>

The result should be loaded here, but I get a blank value:

<main class="<?php echo $mainspan; ?>">
  • There's no question here....... – WillardSolutions Apr 22 '16 at 20:55
  • Instead of span6, span9, or span12 being output into the Main class, the output is blank. – Chris Cather Apr 22 '16 at 22:01
  • @ChrisCather - are you certain that your first batch of code is being run? Add a `var_dump($mainspan)` at the end, and see if anything's output. (Also, is it an issue that the first value you're setting is just `"9"`, and not `"span9"`? – andrewsi Apr 23 '16 at 00:20
  • @andrewsi - I added the function you suggested, but it didn't seem to do anything to the output. Also, I fixed that "9" to "span9" after I posted this. Good spot though. – Chris Cather Apr 23 '16 at 01:59
  • @ChrisCather - if it doesn't print anything, then it looks like your code isn't being called. (To be extra sure - change it to an `exit()`, and see if there's any change.) How are you calling your PHP? – andrewsi Apr 23 '16 at 11:40
  • @andrewsi - I'm sorry, I know just enough PHP to be dangerous. You said to change it to an exit function. Change what exactly to an exit function? Also, not sure what you mean by your question, how I'm calling my PHP. The output is in the second code section I posted. It's an HTML tag calling the mainspan variable inside of the class attribute. `
    `
    – Chris Cather Apr 23 '16 at 19:54
  • @ChrisCather - at the end of the first segment of code, add a line that reads `exit;`. If your website runs OK after that, then the first section isn't being called at all, which means you can narrow down the problem. As to how you're calling it - where is the first piece of code? Is it in a function? Is it in a file you include? – andrewsi Apr 24 '16 at 02:14
  • @andrewsi - Adding the Exit function inside of any of the If statements did stop the page from reading further. So, that suggests the statements are being read, correct? I use the same types of If statements in the Index.php to call the sidebars. Since they work there, I would assume that they're good statements. Therefore, I assume that would mean that it is my call to the variable is flawed. The first piece of code (If statements) is in the Header.php, which the Index.php includes. The second piece of code (variable call) is in the Index.php. – Chris Cather Apr 24 '16 at 20:07
  • There it is! I didn't see the point of loading the header as an include, so I just moved the code into the Index.php and BAM, it works perfectly! Thanks for sticking with me on this andrewsi, and mentioning includes. Wish there was a way to give you props. Still new to Stack Overflow ... obviously. ;-) – Chris Cather Apr 24 '16 at 20:17

1 Answers1

0

Andrewsi asked if there were any includes in the file. That lead me to realize that the code itself was functional, but was not working because one was being included into the other. The IF statements were written in the Header.php, which was included into the Index.php where the variable call was located. Placing the IF statements into the Index.php fixed the issue.

Community
  • 1
  • 1