1

I'm developing Wordpress theme for my friend's company, but this question is all about PHP so writing here, on SO.

I have two sidebars, that's how we display sidebars in Wordpress:

 if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('SidebarName') ) :  
 endif;    

So that's an if statement.

Ok, now, I want to display ONE sidebars IF $layout="one" TWO sidebars IF $layout=="two" etc.

This code works, but it duplicates sidebars contents I believe endifs are messing with the parent loops or something like that:

 if($layout="one") { 

      if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Sidebar 1') ) :  
      endif; 

 } elseif($layout=="two") {

      if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar(' Sidebar 1') ) :  
      endif;    

      if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Sidebar 2') ) :  
      endif;     

 } elseif($layout=="three") { (...)

How to fix that issue? When I just delete the main if loop - everything work like expected, so I'm sure I got lost somewhere above.

Sorry, typos, I'm using $layout== instead of =

2
  • There is nothing like an if loop. What do you mean with main if loop? The outer if statement? What are the functions doing? Seems pretty useless to me (and unintuitive), to have empty if statements (or are you not showing the content of the if statements)? Commented Feb 23, 2011 at 1:24
  • When you say if loops I think you mean nested if statements Commented Feb 23, 2011 at 1:31

5 Answers 5

1

There's no such thing as an "if loop". :)

Since you're basically only repeating the same condition again and again, you can logically restructure it to this:

if (function_exists('dynamic_sidebar')) {
    switch ($layout) {
        case 'one' :
            dynamic_sidebar('Sidebar 1');
            break;
        case 'two' :
            dynamic_sidebar('Sidebar 1');
            dynamic_sidebar('Sidebar 2');
            break;
        case ...
    }
}

This should also take care of the syntax/typo problems that are causing your script to misbehave.

Sign up to request clarification or add additional context in comments.

Comments

1

Edit: I just did a test and it looks like this answer is wrong. I was able to mix colon and brace if and while syntax together. However, the manual does state:

'Note: Mixing syntaxes in the same control block is not supported.'

So I'm a bit confused about this.


PHP If statements have two types of syntax, one with braces, and one with colons.

if () {

}

and

if ():

endif;

There are also similar syntaxes for while, for, foreach etc. But you aren't allowed to mix the braces with the colon syntaxes. So you need to either change your code to

 if($layout=="one") { 

      if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Sidebar 1') ) {
      }

 } elseif($layout=="two") {

      if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar(' Sidebar 1') ) {
      }    

      if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Sidebar 2') ) {
      }     

 } elseif($layout=="three") { (...)

or

 if($layout=="one"):

      if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Sidebar 1') ) :  
      endif; 

 elseif($layout=="two"):

      if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar(' Sidebar 1') ) :  
      endif;    

      if ( !function_exists('dynamic_sidebar') || !dynamic_sidebar('Sidebar 2') ) :  
      endif;     

 elseif($layout=="three"):
  (...)

 endif;

You can read more about it at http://php.net/manual/en/control-structures.alternative-syntax.php

Edit: How silly of me I didn't notice this. Like the other answers say, you have used a single equals sign instead of two to test for equality.

4 Comments

The structure of the code should be fine. I just tried if(1) {if(1): echo "foo"; endif;} and this works.
That's interesting. The manual specifically states: "Note: Mixing syntaxes in the same control block is not supported."
I've also had issues using braces with colon syntax. Maybe it's only an issue with brace ifs inside colon ifs.
I can't find any problem in mixing brace and colon syntaxes. Even with ifs and other control structures like loops. This is really strange, I swear I had issues with this before. I'll edit my post accordingly.
0

I agree with Michael and also, I'm not sure how your functions are set up but usually I'd see something more similar to

if($layout == "one" && function_exists('dynamic_sidebar'))
{
    dynamic_sidebar('Sidebar 1');
}

Comments

0

Looks like you mean to use the equality operator == where you're using the assignment operator =

if ($layout == "one")
// etc

By the way, consider also using the more common if/else PHP syntax. Rather than using the elseif and endif use this syntax. Brace according to your conventions and preferences.

if (condition)
{
  // code for condition
}
else if (condition)
{
  // code
}
else
{
  // else case code
}

Comments

0

First, this is wrong:

if ($layout="one")

Always use == or === in an if statement (same for elseif) because = actually assigns the value. This is probably part of what's causing your bug.

Better yet, in this particular situation, use the switch statement

Third, you don't have to keep copying !function_exists('dynamic_sidebar') just put it in once. It should look like this:

if function_exists('dynamic_sidebar')
{
    switch ($layout)
    {
        case 'one':
        dynamic_sidebar('Sidebar 1');
        break;

        case 'two':
        dynamic_sidebar('Sidebar 1');
        dynamic_sidebar('Sidebar 2');
        break;

        // ...
    }
}

Even better still, change the whole thing to a number and just do a for loop:

if function_exists('dynamic_sidebar')
{
    for ($i = 0; $i < $sidebarCount; ++$i)
    {
        dynamic_sidebar("Sidebar $i");
    }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.