Skip to content

Commit e803def

Browse files
committed
Add rich snippets on post page
1 parent f4c594a commit e803def

File tree

4 files changed

+95
-0
lines changed

4 files changed

+95
-0
lines changed

Block/Post/View/Richsnippets.php

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
/**
3+
* Copyright © 2016 Ihor Vansach ([email protected]). All rights reserved.
4+
* See LICENSE.txt for license details (http://opensource.org/licenses/osl-3.0.php).
5+
*
6+
* Glory to Ukraine! Glory to the heroes!
7+
*/
8+
namespace Magefan\Blog\Block\Post\View;
9+
10+
use Magento\Store\Model\ScopeInterface;
11+
12+
/**
13+
* Blog post view rich snippets
14+
*/
15+
class Richsnippets extends \Magefan\Blog\Block\Post\AbstractPost
16+
{
17+
/**
18+
* @param array
19+
*/
20+
protected $_options;
21+
22+
/**
23+
* Retrieve snipet params
24+
*
25+
* @return array
26+
*/
27+
public function getOptions()
28+
{
29+
if ($this->_options === null) {
30+
$post = $this->getPost();
31+
$this->_options = array(
32+
'@context' => 'http://schema.org',
33+
'@type' => 'BlogPosting',
34+
'@id' => $post->getPostUrl(),
35+
'headline' => $post->getTitle(),
36+
'description' => $post->getMetaDescription(),
37+
'datePublished' => $post->getPublishDate('c'),
38+
);
39+
}
40+
41+
return $this->_options;
42+
}
43+
44+
/**
45+
* Render html output
46+
*
47+
* @return string
48+
*/
49+
protected function _toHtml()
50+
{
51+
return '<script type="application/ld+json">'
52+
. json_encode($this->getOptions())
53+
. '</script>';
54+
}
55+
}

Model/Post.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,10 @@ public function getPostUrl()
191191
return $this->_url->getUrl($this, URL::CONTROLLER_POST);
192192
}
193193

194+
/**
195+
* Retrieve featured image url
196+
* @return string
197+
*/
194198
public function getFeaturedImage()
195199
{
196200
if (!$this->hasData('featured_image')) {
@@ -205,6 +209,25 @@ public function getFeaturedImage()
205209
return $this->getData('featured_image');
206210
}
207211

212+
/**
213+
* Retrieve meta description
214+
* @return string
215+
*/
216+
public function getMetaDescription()
217+
{
218+
$desc = $this->getData('meta_description');
219+
if (!$desc) {
220+
$desc = $this->getData('content');
221+
}
222+
223+
$desc = strip_tags($desc);
224+
if (mb_strlen($desc) > 160) {
225+
$desc = mb_substr($desc, 160);
226+
}
227+
228+
return $desc;
229+
}
230+
208231
/**
209232
* Retrieve post parent categories
210233
* @return \Magefan\Blog\Model\ResourceModel\Category\Collection
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0"?>
2+
<!--
3+
/**
4+
* Copyright © 2016 Ihor Vansach ([email protected]). All rights reserved.
5+
* See LICENSE.txt for license details (http://opensource.org/licenses/osl-3.0.php).
6+
*
7+
* Glory to Ukraine! Glory to the heroes!
8+
*/
9+
-->
10+
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
11+
<body>
12+
<referenceBlock name="head.additional">
13+
<block class="Magefan\Blog\Block\Post\View\Richsnippets" name="blog.post.richsnippets"/>
14+
</referenceBlock>
15+
</body>
16+
</page>

view/frontend/layout/blog_post_view.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
<page xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:View/Layout/etc/page_configuration.xsd">
1111
<update handle="blog_default"/>
1212
<update handle="blog_post_opengraph" />
13+
<update handle="blog_post_richsnippets" />
1314
<body>
1415
<referenceContainer name="content">
1516
<block class="Magefan\Blog\Block\Post\View" name="blog.post" template="post/view.phtml">

0 commit comments

Comments
 (0)