search

Home  >  Q&A  >  body text

Using conditional statements in the middle of joins: a guide

So I have three variables $blue_tooltip_icon, $red_tooltip_icon and $gray_tooltip_icon. Now I want to output specific variables based on the attributes passed to the WordPress shortcode. So if you enter "blue", it's $blue_tooltip_icon; if you enter "red", it's $red_tooltip_icon; if you enter "gray", it's $gray_tooltip_icon.

The question is how to solve this problem. I tried using if statements but found that this is not possible in concatenation.

This is what I'm trying to output via the shortcode, the tooltip icon changes based on the color entered via the shortcode attribute.

$message = '<span data-title="'.$atts['text'].'" class="tooltip">'.$content .$blue_tooltip_icon.'</span>';

P粉164942791P粉164942791485 days ago686

reply all(1)I'll reply

  • P粉704066087

    P粉7040660872023-09-10 13:40:58

    I think this will help you.

    <?php
    // Cleaner way
    
    $blue_tooltip_icon = 'blue';
    $red_tooltip_icon = 'red';
    $green_tooltip_icon = 'green';
    
    $atts['text'] = 'john';
    $content = 'content';
    
    $icons = [
        'blue' => $blue_tooltip_icon,
        'red' => $red_tooltip_icon,
        'green' => $green_tooltip_icon
    ];
    
    $input = 'red';
    
    //$message = '<span data-title="' . $atts['text'] . '" class="tooltip">' . $content . $icons[$input] . '</span>';
    
    // Awkward style with ternary operator
    
    $input = 'green';
    
    $message = '<span data-title="' . $atts['text'] . '" class="tooltip">' . $content . (($input == "red") ? $red_tooltip_icon : (($input == "blue") ? $blue_tooltip_icon : $green_tooltip_icon)) . '</span>';
    
    echo $message;
    
    

    reply
    0
  • Cancelreply