Adding Microdata to Joomla Articles
Google has been using microdata for quite a while now and adding it to your website can help with clickthrough rates from search engine results.
The most obvious is Google’s own authorship markup (not strictly microdata per se) where linking to your Google+ profile adds your profile image to pages in search results, but there are many other tags which can be added to display extra information next to your page title and description.
In this article I am going to look at adding microdata for article publishing times, images and authors to Joomla 3.
Note: If you only want to add Google authorship links then read the post about how to add Google authorship to Joomla articles
I’m not sure how useful adding microdata to an image is but it is an easy example to start with as the property will always be the same. So, overriding the standard article view (templates/mytemplate/html/com_content/article/default.php) find the code which outputs the featured image.
item-image"> image_fulltext_caption): echo 'class="caption"'.' title="' .htmlspecialchars($images->image_fulltext_caption) . '"'; endif; ?> src="image_fulltext); ?>" alt="image_fulltext_alt); ?>"/>
All we need to do is add an itemscope to the parent div and an item property to the image it’s self. You can read more about the subject here http://schema.org/docs/gs.html
item-image" itemscope> image_fulltext_caption): echo 'class="caption"'.' title="' .htmlspecialchars($images->image_fulltext_caption) . '"'; endif; ?> src="image_fulltext); ?>" itemprop="image" alt="image_fulltext_alt); ?>"/>
And that is it. You can test that Google is recieving the correct data using the rich snippets testing tool http://www.google.com/webmasters/tools/richsnippets
Next, lets do the dates. All the information we need is stored in the database so it is just a case of outputting it in the correct format. The standard date bloc looks like this:
get('show_publish_date')) : ?> item->publish_up, JText::_('DATE_FORMAT_LC3'))); ?> get('show_create_date')) : ?> item->modified, JText::_('DATE_FORMAT_LC3'))); ?> get('show_modify_date')) : ?> item->modified, JText::_('DATE_FORMAT_LC3'))); ?>
The date needs to be in the format yyy-mm-dd which we can get by using Joomla’s DATE_FORMAT_LC4. Again, we need to add an itemscope to each of the dates which I’m going to add to the dd tags. Next I’m wrapping the spans with a html5
get('show_publish_date')) : ?> item->publish_up, JText::_('DATE_FORMAT_LC4')); ?>"> item->publish_up, JText::_('DATE_FORMAT_LC3'))); ?> get('show_create_date')) : ?> item->modified, JText::_('DATE_FORMAT_LC4')); ?>"> item->modified, JText::_('DATE_FORMAT_LC3'))); ?> get('show_modify_date')) : ?> item->created, JText::_('DATE_FORMAT_LC4')); ?>"> item->modified, JText::_('DATE_FORMAT_LC3'))); ?>
That takes care of the publishing times. The authorship is a bit more complicated as by default there is no field for a user to add a link to their Google+ profile. It the case of this blog I am the only writer so I can hardcode the author link to my profile.
">http://schema.org/Person"> Written by https://plus.google.com/114266924568110882143" target="_blank" rel="author">
If you have multiple users then you need to go to the contacts component and create a contact for each user and add the Google+ link to the website field (this in its self may be enough for it to work if articles are linked to the profile page). We can then change the link to use this field instead of the standard one and add our microdata.
get('show_author') && !empty($this->item->author )) : ?> ">http://schema.org/Person"> item->created_by_alias ? $this->item->created_by_alias : $this->item->author; $author = ''.$author.''; ?> item->contactid) && $params->get('link_author') == true) : ?> $needle = 'index.php?option=com_contact&view=contact&id=' . $this->item->contactid; $menu = JFactory::getApplication()->getMenu(); $item = $menu->getItems('link', $needle, true); $cntlink = !empty($item) ? $needle . '&Itemid=' . $item->id : $needle; $db = JFactory::getDbo(); $query = 'SELECT `webpage` FROM `#__contact_details` WHERE `id` = '. (int) $this->item->contactid; $db->setQuery($query); $page = $db->loadResult().'?rel=author'; ?> '_blank', 'itemprop' => 'url'))); ?>
So there we have it, microdata added to our Joomla articles. You can add any of the properties mentioned in the schema page http://schema.org/Article
Tip: Test your snippets using the Google rich snippets tool http://www.google.com/webmasters/tools/richsnippets