我使用PHP包含,我需要把头信息放在其中之一。这是可能的吗,或者我只能在index.php的顶部放置一个HEAD部分?
我之所以这样问,是因为PHP包含了一些查询,我需要这些查询才能将OG图像数据(用于社交媒体)放入头部。例如:我有一个文件WEBSHOP.PHP,在这个文件中有一个带有图像的产品。我希望该图像显示在FaceBook的时间线上。
这是我的index.php(缩写版本)的一个示例:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>
<body>
<? include webshop.php; ?>
</body>
这是我的webshop.php(缩写版本)的一个示例:
<!-- some mysql query to get variables as $pic and $row->meta_title -->
<head>
<meta property="og:image" content="http://forteuitgevers.nl/images/boeken/<? echo $pic; ?>" />
<meta property="og:title" content="<? echo $row->meta_title; ?>" />
<meta property="og:description" content="<? echo $row->meta_des; ?>" />
<meta property="og:url" content="http://<? echo $_SERVER['HTTP_HOST']; ?>/<? if (!empty($url_array[1])) { echo $url_array[1]; echo '/' ; } ?><? if (!empty($url_array[2])) { echo $url_array[2] ; } ?>" >
</head>
<!-- some code to view the webshop item -->发布于 2014-12-11 02:27:43
为了将所有的头标记放入一个<head>部分,您必须稍微改变一下PHP文件的结构。如果在开始生成HTML输出之前包含webshop.php文件,则可以在编写head部分时访问这些变量。如下所示:
index.php:
<?php include webshop.php; ?>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
<meta property="og:title" content="<?php echo $row->meta_title; ?>" />
<!-- other meta tags using variables from webshop.php -->
</head>
<body>
<!-- print out HTML code from webshop.php -->
<?php echo $doc_body; ?>
</body>然后,在webshop.php中,您必须使用输出缓冲来保存任何HTML输出,以便您可以将其添加到HTML代码中的适当位置。如下所示:
<?php
// sql queries to get data
ob_start();
?>
<!-- html code to show up in the body section to view webshop items -->
<?php
$doc_body = ob_get_clean();
?>有关ob_start和ob_get_clean的更多信息,请查看Output buffering上的PHP.net手册页面。
发布于 2014-12-11 02:14:04
是的你可以。然而,这是一种糟糕的风格。而且你的HTML是错误的:
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>
<body>
<? include webshop.php; ?>
</body>这将导致
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Untitled Document</title>
</head>
<body>
<head>
<meta property="og:image" content="http://forteuitgevers.nl/images/boeken/<? echo $pic; ?>" />
<meta property="og:title" content="<? echo $row->meta_title; ?>" />
<meta property="og:description" content="<? echo $row->meta_des; ?>" />
<meta property="og:url" content="http://<? echo $_SERVER['HTTP_HOST']; ?>/<? if (!empty($url_array[1])) { echo $url_array[1]; echo '/' ; } ?><? if (!empty($url_array[2])) { echo $url_array[2] ; } ?>" >
</head>
</body>但是,HTML不喜欢head标记在body标记内。但是大多数浏览器仍然可以正确显示它。要确保:使用HTML Validator检查结果。
https://stackoverflow.com/questions/27407275
复制相似问题