vendredi 12 juillet 2019

If statement returning false when it's true

I'm creating a custom element for WPBakery and have mapped out a field like so:

    array(
        'type' => 'dropdown',
        'class' => '',
        'heading' => __( 'Icon', 'text-domain'),
        'param_name' => 'icon',
        'value' => array(
            'Speech mark'=> 'speech',
            'Trophy'=> 'trophy',
            'No icon'=> 'none',
        ),
        'std' => 'speech',
        'admin_label' => true,
        'weight' => 0,
        'group' => __( '', 'my-text-domain' ),
    )

Based on what option is selected from the above dropdown, I want to display to according image:

<?php 
if ($icon == "speech"){
    $icon = get_template_directory_uri().'/assets/src/images/quote.svg';
} 
else if ($icon == "trophy"){
    $icon = get_template_directory_uri().'/assets/src/images/trophy.svg';
} 
else {
    $icon = "";
}
$getIcon = "background: center center no-repeat url(".$icon.") #B2E9E8";
?>

<div class="test">
  <div class="ico" style="<?php echo $getIcon; ?>">
</div>

Results:

  • When selecting "speech mark" from dropdown: Returns no URL (doesn't work)
  • When selecting "trophy" from dropdown: Returns correct URL (works).
  • When selecting "no icon" from dropdown: Returns no URL (works)

Which makes me think that the if is returning false.

I've checked the SVG file name and (as you can see) the param name for speech is correct. So unsure what is going wrong?

A var_dump($icon) with "speech mark" selected from the dropdown returns string(0) ""

Aucun commentaire:

Enregistrer un commentaire