简体   繁体   English

如何在joomla中用php动态设置og:image元标记?

[英]How to dynamically set og:image meta tag with php in joomla?

I'm trying to dynamically set the og:image meta tag for a joomla 1.5 website with a logic on a part of the url. 我正在尝试为joomla 1.5网站动态设置og:image元标记,并在网址的一部分上添加逻辑。

This is the code: 这是代码:

<?php 
$actual_link = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]"; 
if (strpos($actual_link,'basket') !== false) { 
?>

<meta property="og:image" content="http://astel-teglio.it/site/images/Basketball.png" />

<?php
}else{
?>

<meta property="og:image" content="http://astel-teglio.it/site/images/LOGO.jpg" />

<?php
}
?>

The logic is very simple, if the url contains basket I want to use the image Basketball.png, else LOGO.jpg. 逻辑很简单,如果URL包含购物篮,则我要使用图像Basketball.png,否则要使用LOGO.jpg。

If I watch the source code in the browser I see the right attribute but when a paste the link on facebook I always see the Logo image (else case). 如果我在浏览器中看到源代码,我会看到正确的属性,但是在Facebook上粘贴链接时,我总是会看到徽标图像(其他情况)。

There are the two sample pages: 有两个示例页面:

In Joomla you can dynamically set og:attributes or meta tags like below. 在Joomla中,您可以像下面那样动态设置og:attributes或meta标签。

$doc =& JFactory::getDocument();
if($shareTitle != '')
    $doc->addCustomTag( '<meta property="og:title" content="'.$shareTitle.'" />'  );
if($shareDesc != '')
    $doc->addCustomTag( '<meta property="og:description" content="'.$shareDesc.'" />' );
if($shareImg != '')
    $doc->addCustomTag( '<meta property="og:image" content="'.$shareImg.'" />' );

You can also check How to set meta data of a page in joomla . 您还可以在joomla中检查如何设置页面的元数据

Also facebook have cache issue, you can debug here . 另外,facebook也有缓存问题,您可以在此处进行调试。

Hope this helps.. 希望这可以帮助..

I think that the problem is caused by the SEF URLs that change my url from: 我认为问题是由将我的网址从以下位置更改的SEF网址引起的:

http://astel-teglio.it/site/index.php?option=com_content&view=article&id=317

to: 至:

http://astel-teglio.it/site/it/minibasket/stagione-20132014/scoiattoli/317--morbegno--molino-tudori-teglio-12-12.html

but when facebook loads the meta tags it load the original url that hasn't basket in the url string and so it loads the standard image. 但是当facebook加载meta标签时,它会加载url字符串中没有包含篮子的原始url,因此它会加载标准图片。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM