1

My code only executes the if statement, but does not continue to execute the else part. Ive tried each statement individually and they work fine. But, when I put them together, only the first statement is executed. What am I doing wrong?

I've tried this:

function body_google_webfonts() {
    if( $settings['font_type_body'] = "Handlee") :
        wp_register_style('body-google-webfonts', 'http://fonts.googleapis.com/css?family=Handlee');
        wp_enqueue_style( 'body-google-webfonts');
    elseif ( $settings['font_type_body'] = "Kreon") :
        wp_register_style('body-google-webfonts', 'http://fonts.googleapis.com/css?family=Kreon');
        wp_enqueue_style( 'body-google-webfonts');
    endif;
}

and this:

function body_google_webfonts() {
    if( $settings['font_type_body'] = "Handlee") :
        wp_register_style('body-google-webfonts', 'http://fonts.googleapis.com/css?family=Handlee');
        wp_enqueue_style( 'body-google-webfonts');
    endif;
    if ( $settings['font_type_body'] = "Kreon") :
        wp_register_style('body-google-webfonts', 'http://fonts.googleapis.com/css?family=Kreon');
        wp_enqueue_style( 'body-google-webfonts');
    endif;
}

and this:

function body_google_webfonts() {
    if( $settings['font_type_body'] = "Handlee") {
        wp_register_style('body-google-webfonts', 'http://fonts.googleapis.com/css?family=Handlee');
        wp_enqueue_style( 'body-google-webfonts');
    }
    if ( $settings['font_type_body'] = "Kreon") {
        wp_register_style('body-google-webfonts', 'http://fonts.googleapis.com/css?family=Kreon');
        wp_enqueue_style( 'body-google-webfonts');
    }
}

and I've tried this:

function body_google_webfonts() {
    if( $settings['font_type_body'] = "Handlee") {
        wp_register_style('body-google-webfonts', 'http://fonts.googleapis.com/css?family=Handlee');
        wp_enqueue_style( 'body-google-webfonts');
  } else if ( $settings['font_type_body'] = "Kreon") {
        wp_register_style('body-google-webfonts', 'http://fonts.googleapis.com/css?family=Kreon');
        wp_enqueue_style( 'body-google-webfonts');
  }
}
3
  • 1
    wow use double == note one = Commented Feb 1, 2013 at 18:12
  • 2
    I swear, there are a million of these questions here! == and =. Commented Feb 1, 2013 at 18:13
  • When you code 12 hours a day, it's SO easy to miss the littlest things. My brain needs a vacation :P Thanks guys. Commented Feb 1, 2013 at 18:44

2 Answers 2

4

You assigning a value to a variable instead of comparing it. That will always result in the statement being true.

if( $settings['font_type_body'] = "Handlee") :

should be

if( $settings['font_type_body'] == "Handlee") :

or

if( $settings['font_type_body'] === "Handlee") :
Sign up to request clarification or add additional context in comments.

2 Comments

John I think one comment is enough!
My goodness. Thanks! I feel like an idiot for not catching that. Too bad, debug mode doesn't catch things like this, ha!
0

I think your problem is: you are not comparing two things, you are making the variable settings equal to "Handlee". This operation will always return true, so just the first if will be executed.

To solve this, use the correct sentence:

$settings['font_type_body'] == "Handlee"

With double "=".

@Jessica, a very good programation practice, to avoid this simple error, is always put your variable at right side of the comparation. Something like this:

int a = 0; 
if 0 == a 

You can use it for others languages. It's good cause, if you try to do

If 0 = a

it's easy to find your problem in debug mode.

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.