我已经建立了一个使用octobercms的博客和plugin rainlab博客。现在的问题是,我想使用特色图片作为facebook分享图片。
如何做到这一点?当我转到Facebook调试器时,我可以看到徽标和我的特色图像,但一旦我分享了它,显示的图像就是徽标。
非常感谢
发布于 2015-06-25 15:57:26
在您的博客文章所使用的页面/布局中,您将需要添加OpenGraph标记(如果尚未添加)。
<meta property="og:url" content="http://www.yourwebsite.com" />
<meta property="og:type" content="website" />
<meta property="og:image" content="{{ this.getShareImageUrl }}" />
<meta property="og:title" content="Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod" />
<meta property="og:description" content="Lorem ipsum dolor sit amet, consectetur adipisicing elit. Obcaecati modi illo explicabo quidem ipsa, voluptatum quibusdam, nemo veritatis enim eveniet doloribus et eius voluptate nam cum veniam, fugit. Dolorum, adipisci." />Facebook需要这些标签来提取正确的数据。您可能会发现this对阅读有关这些标记的更多信息很有用。
因此,您现在需要的只是正确的featured_image的url,因为您很可能有多个featured_image。您可以选择随心所欲地编写该逻辑(因此,我在上面的代码中只使用了一个方法调用),但是一旦您拥有了所需的featured_image实例,就可以在该实例上使用getPath()方法来获取url (因为它是File类的一个实例,所以您可以在该实例上使用这些方法)。
您可以在此处阅读更多文档- http://octobercms.com/docs/database/model#file-attachments
就是这样,返回到FB调试器并按debug。如果它不起作用,试着使用另一个按钮,上面写着‘抓取新的抓取信息’(只在你已经按了一次调试按钮之后才会出现),你应该是很好的。
我刚刚完成了创建一个新网站的这些步骤,我不得不让facebook重新抓取它才能正常工作。请让我们知道这是否适用于您。
发布于 2017-06-14 22:48:54
这可能会很晚,但我也遇到了同样的问题,并使用以下代码将图片从您的博客页面传递到布局文件中:
function onEnd() {
$_post = $this->components['blogPost'];
if ($_post->featured_images->first()) {
$this->page->og_image = $_post->featured_images->first()->path;
}
}这将允许您在布局文件中使用此标记(使用您的<head> ):
{% if this.page.og_image %}
<meta property="og:image" content="{{ this.page.og_image }}" />
{% else %}https://stackoverflow.com/questions/30936835
复制相似问题