我刚刚在我的新闻.php页面上添加了Facebook元标签。此文件还动态生成其下的文章 (url) 页面。但是我如何在头部设置 fb 元标记的标题(即。 <meta property="og:title" content="<?php echo $data[0]['title']; ?>" />
) 当$data在身体的下方被定义/填充时?
这么多网站是怎么做到的?
如能就此提供任何指导,将不胜感激。
以下是我的页面的代码:
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Title</title>
<meta property="fb:app_id" content="6666666666666"/>
<meta property="og:title" content="<?php echo $data[0]['title']; ?>" />
</head>
<body>
<script>
window.fbAsyncInit = function() {
FB.init({
appId : '66666666666666',
xfbml : true,
version : 'v2.5'
});
};
(function(d, s, id){
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) {return;}
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/sdk.js";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
</script>
<header>
<div class="contentwrap">
<article>
<h1>- News- </h1>
<?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>
<div
class="fb-like"
data-share="true"
data-width="450"
data-show-faces="false">
</div>
<?php }
else {
(continues)
我想将
您的网站切换到mvc
结构的最佳选择是详细的文章,mvc pattern
:http://www.sitepoint.com/the-mvc-pattern-and-php-1/
现在,在没有重大更改的情况下,您可以更改yoru news.php
结构,以便它将在html部分之前检索顶部的所有内容php数据,因此在这种情况下,您将在html的所有位置拥有$news
变量,并且可以轻松地创建mattes
并在body
中显示内容
您必须在整个站点中使用输出缓冲控件来处理此问题。
基本上ob_start()
文件的第一个生命,ob_get_clean()
检索执行期间输出的所有内容。