Skip to content

Instantly share code, notes, and snippets.

@m4munib
Last active December 23, 2021 13:21
Show Gist options
  • Save m4munib/e128216ec65415e6a1b94e1d495631d9 to your computer and use it in GitHub Desktop.
Save m4munib/e128216ec65415e6a1b94e1d495631d9 to your computer and use it in GitHub Desktop.
<?php
// Usage:
$post_id = 'POST_ID_HERE';
echo idx_get_grouped_features( $post_id );
/**
Example Output:
<div class="idx_grouped_features">
<h4>Interior</h4>
<ul>
<li>General: Eat-in Kitchen</li>
<li>Appliances: Dishwasher, Disposal, Microwave, Range, Refrigerator</li>
<li>Flooring: Tile</li>
<li>A/C: Central Air</li>
<li>Heating: Central</li>
</ul>
<h4>Exterior</h4>
<ul>
<li>General: Block, Stucco, Fence, Sidewalk</li>
</ul>
<h4>Construction</h4>
<ul>
<li>Roofing: Shingle</li>
</ul>
<h4>Location</h4>
<ul>
<li>HOA Amenities: Pool, 44.0, Monthly</li>
</ul>
<h4>Lot</h4>
<ul>
<li>Zoning: R-2</li>
</ul>
</div>
*/
function idx_get_grouped_features( $post_id ){
$key = '_ct_grouped_features';
$grouped_features = get_post_meta( $post_id, $key, true );
$output = '';
if( '' === $grouped_features ){
return $output;
}
$grouped_features = apply_filters( 'idx_get_grouped_features', json_decode( $grouped_features, true ), $grouped_features );
if( is_array( $grouped_features ) && ! empty($grouped_features) ){
$output = '<div class="idx_grouped_features">';
foreach( $grouped_features as $feature_group => $features_data ){
$output .= '<h4>'. esc_html( $feature_group ) .'</h4>';
$output .= '<ul>';
foreach( $features_data as $feature_section => $features ){
$feature_parts = explode(',', $features );
$feature_parts = array_map('trim', $feature_parts);
$output .= '<li>'. esc_html( $feature_section ) .': '.implode(', ', $feature_parts).'</li>';
}
$output .= '</ul>';
}
$output .= '</div>';
}
return $output;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment