Skip to content

Instantly share code, notes, and snippets.

@g-maclean
Last active November 4, 2025 13:51
Show Gist options
  • Save g-maclean/c8f9ad636ddeb54cb4c40f9ecec1dd3a to your computer and use it in GitHub Desktop.
Save g-maclean/c8f9ad636ddeb54cb4c40f9ecec1dd3a to your computer and use it in GitHub Desktop.
Property Hive - shortcode to output address without number
function ph_address_no_number_shortcode( $atts ) {
global $property;
// Do nothing if $property object isn't available
if ( ! isset( $property ) || ! is_object( $property ) ) {
return '';
}
// Shortcode attributes: separator, optional HTML tag, optional class
$atts = shortcode_atts( array(
'separator' => ', ',
'tag' => '', // e.g., 'span', 'div', 'p', leave empty for no wrapper
'class' => '', // optional CSS class
), $atts, 'ph_address_no_number' );
// Address fields to include (excluding building/house number)
$fields = array(
'address_street',
'address_two',
'address_three', // Town/City
'address_four', // County/State
'address_postcode',
'address_country',
);
$parts = array();
foreach ( $fields as $field ) {
// Directly get the value, cast to string, trim whitespace
$val = trim( (string) $property->$field );
// Only add non-empty values
if ( $val !== '' ) {
$parts[] = $val;
}
}
if ( empty( $parts ) ) {
return '';
}
$output = implode( $atts['separator'], $parts );
// Wrap in HTML tag if specified
if ( ! empty( $atts['tag'] ) ) {
$class_attr = $atts['class'] ? ' class="' . esc_attr( $atts['class'] ) . '"' : '';
$output = '<' . esc_html( $atts['tag'] ) . $class_attr . '>' . esc_html( $output ) . '</' . esc_html( $atts['tag'] ) . '>';
}
return $output;
}
add_shortcode( 'ph_address_no_number', 'ph_address_no_number_shortcode' );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment