问题
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