【问题标题】:How to dynamically modify the content of the meta property="og:image" in PHP?如何动态修改 PHP 中 meta 属性="og:image" 的内容?
【发布时间】:2021-01-18 06:16:02
【问题描述】:

我正在使用 PHP 和 Smarty 模板引擎。

我需要创建一个函数或找到某种方法,每次我分享视频(即包含视频的社交网络帖子)时,我都会获取该视频缩略图的值(缩略图的位置存储在数据库中)并将其插入到元属性 =“og:image”的内容中,这样当我分享它时(例如,通过 Whatsapp),它会显示该视频的缩略图而不是默认图像。

有什么想法吗?提前致谢。

【问题讨论】:

  • 我理解正确吗?视频显示在您自己的网站上,并且您有包含元数据的数据库?然后只是呼应相关的富媒体 sn-p 。和视频一起。

标签: php smarty


【解决方案1】:

如果您每页只有一个视频,这很容易。

  1. 从数据库中获取数据

2.) 在相应的 HTML 字段中回显检索到的数据

例如

普通 PHP

<meta property="og:title" content="<?=  $title ?>" >
<meta property="og:description" content="<?= $decription  ?>">
<meta property="og:type" content="website" />
<meta property="og:url" content="<?= $_SERVER['PHP_SELF']; ?>" >
<meta property="og:image" content="<?= $thumbnail_url ?>" >
<meta property="og:site_name" content="your site name" >
<meta property="og:locale" content="es_ES" >
<meta property="og:locale:alternate" content="en_US" >
<meta property="fb:app_id" content="xxxxxxxxxxxxxxxxx" >
<meta property="fb:admins" content="yyyyyyyyyyyyyyyyyyy" >

在 SMARTY 中,您只需将模板中的 替换为 {$var}

<meta property="og:title" content="{$title}" >
    <meta property="og:description" content="{$decription}">
    <meta property="og:type" content="website" />
    <meta property="og:url" content="{$_SERVER['PHP_SELF']}" >
    <meta property="og:image" content="{$thumbnail_url}" >

3.) 使用 fb Share Debugger 对其进行测试 https://developers.facebook.com/tools/debug/

【讨论】:

    猜你喜欢
    • 2017-11-17
    • 1970-01-01
    • 2011-03-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-04
    • 2011-02-08
    • 2017-09-21
    相关资源
    最近更新 更多