Unable to echo dynamic data in facebook metatags

ぐ巨炮叔叔 提交于 2020-01-17 04:58:06

问题


I've successfully added facebook's social plugin to my news page (which itself dynamically creates all the article pages which appear under it). I've added metatags however cannot understand how to echo dynamic data within the metatags, especially since that data is generated later in the code. ie. My fb metatags are within the head of my news.php

<head>   ... *(other stuff)*
<meta property="og:title" content="When Great Minds Go To Sleep" />
</head>

However it doesn't make sense for me to statically provide this content since there are many news article pages that will be generated other than this. I want the content to be picked up from what is generated dynamically later in the file (after all this is one of many news pages). ie. I want to be able to say, content="<?php echo $data[0]['title']; ?>". However the $data array is generated only later in news.php, at the start of the body:

<?php 
                if (isset($_REQUEST['id'])) {// user gets here by clicking on link with id
                    $id_raw = trim(htmlentities($_REQUEST["id"]));
                    $data = news_data_id($conn, $id_raw);
                    ?>          

                    <h2><?php echo $data[0]['title']; ?></h2>
                    <img src="<?php echo $data[0]['filepath'];?>" style="float: left; max-height: 5em; margin: 0.5em 0.5em 0.5em 0; -webkit-shape-outside: circle(50%); shape-outside: circle(50%);">
                    <p><?php echo $data[0]['body']; ?></p>

So when I do try

<meta property="og:title" content="<?php echo $data[0]['title']; ?>" />

nothing turns up except the default 'Title'.

How can this be addressed then? Would appreciate any guidance on this issue.

来源:https://stackoverflow.com/questions/35473703/unable-to-echo-dynamic-data-in-facebook-metatags

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!