Inventory_Presser_Schema_Org_Generator::schema_org_json_ld( int $post_ID )
Returns Schema.org markup for this Vehicle as a JSON-LD code block
Parameters Parameters
- $post_ID
-
(int) (Required) The current post ID.
Return Return
(string) Schema.org JSON script element
Source Source
File: includes/class-schema-org-generator.php
protected function schema_org_json_ld( $post_ID ) {
$obj = array(
'@context' => 'https://schema.org/',
'@type' => 'Vehicle',
);
$obj['name'] = wp_strip_all_tags( get_the_title( $post_ID ) );
$make = invp_get_the_make( $post_ID );
if ( '' !== $make ) {
$obj['brand'] = array(
'@type' => 'Thing',
'name' => wp_strip_all_tags( $make ),
);
}
$vin = invp_get_the_VIN( $post_ID );
if ( '' !== $vin ) {
$obj['vehicleIdentificationNumber'] = wp_strip_all_tags( $vin );
}
$year = invp_get_the_year( $post_ID );
if ( 0 !== $year ) {
$obj['vehicleModelDate'] = $year;
}
// Do we have photos?
if ( 0 < invp_get_the_photo_count( $post_ID ) ) {
$obj['image'] = esc_url_raw( invp_get_the_photo_url( $post_ID ) );
}
$odometer = invp_get_the_odometer( '', $post_ID );
if ( '' !== $odometer ) {
// Extract just digits from the odometer value.
$odometer_digits = abs( (int) filter_var( $odometer, FILTER_SANITIZE_NUMBER_INT ) );
$obj['mileageFromOdometer'] = array(
'@type' => 'QuantitativeValue',
'value' => $odometer_digits,
'unitCode' => 'SMI',
);
}
if ( '' !== invp_get_the_engine( $post_ID ) || '' !== invp_get_the_fuel( $post_ID ) ) {
$obj['vehicleEngine'] = array();
if ( '' !== invp_get_the_engine( $post_ID ) ) {
$obj['vehicleEngine']['engineType'] = wp_strip_all_tags( invp_get_the_engine( $post_ID ) );
}
if ( '' !== invp_get_the_fuel( $post_ID ) ) {
$obj['vehicleEngine']['fuelType'] = wp_strip_all_tags( invp_get_the_fuel( $post_ID ) );
}
}
if ( '' !== invp_get_the_body_style( $post_ID ) ) {
$obj['bodyType'] = wp_strip_all_tags( invp_get_the_body_style( $post_ID ) );
}
if ( '' !== invp_get_the_color( $post_ID ) ) {
$obj['color'] = wp_strip_all_tags( invp_get_the_color( $post_ID ) );
}
if ( '' !== invp_get_the_interior_color( $post_ID ) ) {
$obj['vehicleInteriorColor'] = wp_strip_all_tags( invp_get_the_interior_color( $post_ID ) );
}
if ( invp_get_the_description( $post_ID ) ) {
$obj['description'] = wp_strip_all_tags( invp_get_the_description( $post_ID ) );
}
$schema_drive_type = $this->schema_org_drive_type( invp_get_the_drive_type( $post_ID ) );
if ( null !== $schema_drive_type ) {
$obj['driveWheelConfiguration'] = $schema_drive_type;
}
if ( '' !== invp_get_the_transmission( $post_ID ) ) {
$obj['vehicleTransmission'] = wp_strip_all_tags( invp_get_the_transmission( $post_ID ) );
}
return '<script type="application/ld+json">' . wp_json_encode( $obj, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE ) . '</script>';
}
Expand full source codeCollapse full source codeView on Github