Using conditional statements in the middle of joins: a guide
P粉164942791
P粉164942791 2023-09-09 23:23:25
0
1
647

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粉164942791
P粉164942791

reply all(1)
P粉704066087

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;

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template