0

My shortcode includes a switch statement, that outputs a one-word weather summary. This displays correctly when I swap $summary = for return, but I cannot seem to get it working with a <span> or link around the content.

What am I missing here?

switch ( $weather['icon'] ) {

    case 'clear-day': case 'clear-night': 
        $summary = 'sunny'; 
        break;
    case 'wind': 
        $summary = 'breezy'; 
        break;
    case 'rain': 
        $summary = 'drizzly'; 
        break;              
    case 'fog': 
        $summary = 'foggy'; 
        break;
    case 'cloudy': 
        $summary = 'murky'; 
        break;
    case 'partly-cloudy-day': case 'partly-cloudy-night': 
        $summary = 'cloudy'; 
        break;
    case 'snow': case 'sleet':
        $summary = 'snowy'; 
        break;
    default: 
        $summary = 'beautiful'; 
        break;

    return '<span class="weather">'.$summary.'</span>';

} // end switch
1

Get out of the switch block before placing the final return statement.

if you place return at the end of the switch block that way, the switch block already ends before it gets to the return statement (because of the previous matching break statements).

The correct CODE will be:

// just in case $summary is not defined out side of the switch block
$summary = '';
switch ( $weather['icon'] ) {

    case 'clear-day':
    case 'clear-night': 
        $summary = 'sunny'; 
        break;
    case 'wind': 
        $summary = 'breezy'; 
        break;
    case 'rain': 
        $summary = 'drizzly'; 
        break;              
    case 'fog': 
        $summary = 'foggy'; 
        break;
    case 'cloudy': 
        $summary = 'murky'; 
        break;
    case 'partly-cloudy-day': case 'partly-cloudy-night': 
        $summary = 'cloudy'; 
        break;
    case 'snow': case 'sleet':
        $summary = 'snowy'; 
        break;
    default: 
        $summary = 'beautiful'; 
        break;

} // end switch

return '<span class="weather">'.$summary.'</span>';

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.