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');
}
}
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:
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:
You can use it for others languages. It’s good cause, if you try to do
it’s easy to find your problem in debug mode.