Foreach PHP loop to customize output of taxonomy or field that is saved as an array

content-single-job_listing.php

<?php

$sh_indoor_outdoor = get_job_field( 'indoor_outdoor' );

if( is_array( $sh_indoor_outdoor ) && ! empty( $sh_indoor_outdoor ) ){

	foreach( $sh_indoor_outdoor as $value ){
		
		// Go to next item if $value is an empty string, 0, false, or empty array
		if( empty( $value ) ) continue;
		
		// This switch statement checks the value to see what the class needs to be set to
		// for the icon based on the value.  You can add more by copy paste the "case" code block
		// set the default to an icon you want to use if the value does not match any of your case statements
		switch ($value) {
		    case 'pools':
		        $icon = 'icon-pools';
		        break;
		    case 'bars':
		        $icon = 'icon-bars';
		        break;
	            default:
	        	$icon = 'icon-default';
		}
		
		// This will output HTML including icon class, value of $icon, then actual value, followed by a line break <br />
		echo "<i class="icon {$icon}"></i>{$value}<br />";
	}

}
No comments yet.

Leave a Reply