As it's also mentioned in the comments (tooltip) of the GSD type dropdown, some GSD types require additional code on your part. But it's not hard.
Here are the steps.
0. Upgrade to K2 v2.10.3 (dev) from
getk2.org/downloads/?f=K2_Development_Release.zip - this is required to be able to get the raw URL from a "link" type extra field. This step will not be necessary once K2 v2.10.3 is officially released.
1. Assuming you have a specific category for events, first select the "event" GSD type for that category, in the 3rd tab of the category settings.
2. Create an override for item.php for that category. Copy the file /components/com_k2/templates/default/item.php into /templates/YOUR_TEMPLATE/html/com_k2/events/ (create the additional folders after /templates/YOUR_TEMPLATE/html/).
3. Go back to your events K2 category settings and select the new "events" sub-template (like so:
jmp.sh/4TJsUWB). This will only override the item view. If you want to override the category listing as well, just copy all the files in /components/com_k2/templates/default/ into /templates/YOUR_TEMPLATE/html/com_k2/events/. More info on overriding K2 can be found here:
getk2.org/documentation/tutorials/templating-with-k2-and-the-concepts-of-sub-templates
4. Create a new K2 Extra Fields Group and call it "Events".
5. Create extra fields for each of the GSD data you wish to add. According to
developers.google.com/search/docs/data-types/event the bare minimum for a GSD event are "location" (object), "name" (we can use the K2 item's title here), "startDate" and optionally "endDate" and "offers" (object) as you requested. "description" and "image" already come with default K2 GSD. For any object element referenced, you'll have to create an extra field for each of its properties. So for "location" and given that GSD guidelines reference this:
"location": {
"@type": "Place",
"name": "Snickerpark Stadium",
"address": {
"@type": "PostalAddress",
"streetAddress": "100 West Snickerpark Dr",
"addressLocality": "Snickertown",
"postalCode": "19019",
"addressRegion": "PA",
"addressCountry": "US"
}
}
...you would need to add the following ("text" type) extra fields:
- Location Name (with extra field alias 'e_loc_name')
- Location Address (with extra field alias 'e_loc_addr')
- Location Town/City (with extra field alias 'e_loc_city')
- Location Postal Code (with extra field alias 'e_loc_pc')
- Location State/Province (with extra field alias 'e_loc_state')
- Location Country (with extra field alias 'e_loc_country')
Additionally, for the rest of the fields
- Start Date ("date" type extra field and alias 'e_start_date')
- End Date ("date" type extra field and alias 'e_end_date')
- Ticket Price ("text" type extra field and alias 'e_ticket_price')
- Ticket Purchase URL ("link" type extra field and alias 'e_ticket_link')
6. You will fetch each of these extra fields separately (as described here as well
getk2.org/documentation/tips-a-tricks/display-single-extra-fields-anywhere-in-your-k2-content) and build your new GSD properties.
Your default K2 GSD look like this:
jmp.sh/gmcOnu3 (or just do a view source on
getk2.org/blog/k2-v2102-released-now-with-a-100-mobile-friendly-backend-user-interface).
We will change and extend these using the following PHP code added in your item.php override, right after the "defined('_JEXEC') or die;" part:
// Get current GSD
$getItemGSD = $this->params->get('itemGoogleStructuredData');
// Search and replace GSD
$document = JFactory::getDocument();
foreach ($document->_script as $type => $script) {
if ($type == 'application/ld+json' && $script == $getItemGSD) {
// Remove current GSD from the <head>
unset($document->_script);
$itemGSD = json_decode($getItemGSD);
// Remove any default K2 GSD properties we don't want for the GSD "event" type
unset($itemGSD->articleBody);
unset($itemGSD->articleSection);
unset($itemGSD->author);
unset($itemGSD->dateModified);
unset($itemGSD->datePublished);
unset($itemGSD->headline);
unset($itemGSD->keywords);
unset($itemGSD->publisher);
// Append new GSD data as object properties to '$itemGSD'
$itemGSD->location = new stdClass;
$itemGSD->location->{'@type'} = 'Place';
$itemGSD->location->name = $this->item->extraFields->e_loc_name->value;
$itemGSD->location->address = new stdClass;
$itemGSD->location->address->{'@type'} = 'PostalAddress';
$itemGSD->location->address->streetAddress = $this->item->extraFields->e_loc_addr->value;
$itemGSD->location->address->addressLocality = $this->item->extraFields->e_loc_city->value;
$itemGSD->location->address->postalCode = $this->item->extraFields->e_loc_pc->value;
$itemGSD->location->address->addressRegion = $this->item->extraFields->e_loc_state->value;
$itemGSD->location->address->addressCountry = $this->item->extraFields->e_loc_state->value;
$itemGSD->name = $this->item->title;
$itemGSD->startDate = $this->item->extraFields->e_start_date->value;
$itemGSD->endDate = $this->item->extraFields->e_end_date->value;
$itemGSD->offers = new stdClass;
$itemGSD->offers->{'@type'} = 'Offer';
$itemGSD->offers->price = $this->item->extraFields->e_ticket_price->value;
$itemGSD->offers->priceCurrency = 'USD'; // Use EUR or other currency here based on your needs
$itemGSD->offers->url = $this->item->extraFields->e_ticket_link->rawValue;
// Re-insert GSD data with new additions
$itemGSD = json_encode($itemGSD, JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
$document->addScriptDeclaration($itemGSD, 'application/ld+json');
}
}
7. Now start filling up the event data (extra fields) in the K2 items of yours Events category.
In the end you'll see something like this on each event K2 item's page source:
jmp.sh/qUvDXud (wherever you see "null" is where I just didn't add data).
8. Finally test one of your K2 items on:
search.google.com/structured-data/testing-tool - this tool will tell you if you need to change anything or if some property is missing.