Skip to content

Instantly share code, notes, and snippets.

@volodymyrpekh
Created September 27, 2017 23:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save volodymyrpekh/a266f07f18308065dbc41cb3341aaab2 to your computer and use it in GitHub Desktop.
Save volodymyrpekh/a266f07f18308065dbc41cb3341aaab2 to your computer and use it in GitHub Desktop.
Listify JSON-LD - Full version
//To be put in the child theme functions.php
apply_filters( 'listify_get_listing_jsonld', 'my_listify_get_listing_jsonld' );
function my_listify_get_listing_jsonld($markup, $listing) {
$markup = array();
$location_data = $listing->get_location_data();
$markup = array(
'@context' => 'http://schema.org',
'@type' => 'Place',
'@id' => $listing->get_permalink(),
'name' => $listing->get_title(),
'description' => $listing->get_short_description(),
'url' => array(
'@type' => 'URL',
'@id' => $listing->get_permalink(),
),
'hasMap' => $listing->get_map_url(),
);
// Location.
if ( $location_data['address_1'] ) {
$markup['address'] = array(
'@type' => 'PostalAddress',
);
if ( '' !== $location_data['city'] ) {
$markup['address']['addressLocality'] = $location_data['city'];
}
if ( '' !== $location_data['state'] ) {
$markup['address']['addressRegion'] = $location_data['state'];
}
if ( '' !== $location_data['postcode'] ) {
$markup['address']['postalCode'] = $location_data['postcode'];
}
if ( '' !== $location_data['full_country'] ) {
$markup['address']['addressCountry'] = array(
'@type' => 'Text',
'@id' => $location_data['full_country'],
);
}
if ( '' !== $location_data['address_1'] ) {
$markup['address']['streetAddress'] = $location_data['address_1'] . ( '' !== $location_data['address_2'] ? ( ' ' . $location_data['address_2'] ) : '' );
}
}
// Geolocation.
if ( $listing->get_lat() ) {
$markup['geo'] = array(
'@type' => 'GeoCoordinates',
'latitude' => $listing->get_lat(),
'longitude' => $listing->get_lng(),
);
}
// Ratings.
if ( 0 !== $listing->get_rating_count() ) {
$markup['aggregateRating'] = array(
'@type' => 'AggregateRating',
'ratingValue' => $listing->get_rating_average(),
'ratingCount' => absint( $listing->get_rating_count() ),
'bestRating' => absint( $listing->get_rating_best() ),
'worstRating' => absint( $listing->get_rating_worst() ),
);
}
// Image.
$image = wp_get_attachment_image_src( get_post_thumbnail_id( $listing->get_id() ) );
if ( false !== $image ) {
$markup['image'] = array(
'@type' => 'URL',
'@id' => $image[0],
);
}
// Logo.
$logo = $listing->get_secondary_image();
if ( false !== $logo ) {
$markup['logo'] = array(
'@type' => 'URL',
'@id' => $logo,
);
}
// Main entity.
if ( $listing->get_url() ) {
$markup['mainEntityOfPage'] = array(
'@type' => 'WebPage',
'@id' => $listing->get_url(),
);
}
return $markup;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment