简体   繁体   English

PHP包含 <head> 和社交媒体标签

[英]PHP Include <head> and Social Media tags

I created a family website. 我创建了一个家庭网站。 I have the header for the entire site all the way through my main as a PHP INCLUDE. 我一直将整个站点的头作为PHP INCLUDE贯穿整个主站点。 This is a must because I have a massive directory of fly down menus that need to change across the site when I change that one file. 这是必须的,因为我有一个庞大的下拉菜单目录,当我更改一个文件时,需要在整个站点上进行更改。

Everything works beautifully except for one thing: I have unique articles where only on those pages I need unique meta tag OG information specific to that one page so the correct information is displayed when the article is shared on Facebook, Twitter, etc. When you use a PHP include across the entire site for the header, how do you change the unique meta OG data for select pages only? 除一件事情外,一切工作正常:我有独特的文章,只有在那些页面上,我才需要特定于该页面的独特的meta标签OG信息,因此当在Facebook,Twitter等上共享该文章时,会显示正确的信息。整个站点上都包含一个PHP的标头,那么如何仅更改所选页面的唯一元OG数据?

I know putting a 2nd head tag wouldn't be ideal. 我知道放置第二个头部标签并不理想。 Also, I would have to put it in the body given my include file for the header includes the top body tag. 此外,鉴于头文件包含top body标签,我必须将其放入正文中。

Before I went and changed the coding logic of the site, I was hoping there is a straightforward solution to this I am no aware of (and I am a noob coder so that may be part of it). 在我去改变站点的编码逻辑之前,我希望有一个我不知道的简单解决方案(而且我是一个noob编码器,所以可能是其中的一部分)。

I know I could put the main menu only in a file, and then call that menu file in a 2nd header file that is then itself called in an include across the entire site.. that would give me the latitude in those pages. 我知道我可以只将主菜单放在一个文件中,然后在第二个头文件中调用该菜单文件,然后在整个站点的包含文件中本身调用第二个头文件。这将使我在那些页面中拥有更大的自由度。 But was hoping there is an easier answer like, "oh no, they meta OG tags can go in the body like this...." or something? 但是,是否希望有一个更简单的答案,例如“哦,不,它们的元OG标签可以像这样进入人体……”或其他什么?

I would recommend setting the desired header information in PHP variables at the top of the page, and then include the PHP header file: 我建议在页面顶部的PHP变量中设置所需的标头信息, 然后包含PHP标头文件:

page.php : page.php

<?php
$og_url = 'http://www.example.com';
include('header.php');
?>

header.php : header.php

<html>
  <head>
    <meta property="og:url" content="<?php echo $og_url; ?>" />
  </head>

This way, you can set unique social media information for each page, while still making use of the global <head> section from the header file. 这样,您可以为每个页面设置唯一的社交媒体信息,同时仍然使用头文件中的全局<head>部分。 Keep in mind that you'll probably also want fallback behaviour in case your variables aren't referenced on every page that you call the header file from: 请记住,如果未在从以下位置调用头文件的每个页面上都引用变量,则可能还需要回退行为:

<meta property="og:url" content="<?php echo isset($og_url) ? $og_url : $_SERVER['DOCUMENT_ROOT']; ?>" />

While a document has to start with <html> , it's perfectly fine to set PHP variables before calling <html> ; 虽然文档必须以<html>开头,但是在调用<html>之前设置PHP变量是完全可以的; it's only content that's actually written to the DOM that you need to worry about. 它只是您真正需要担心写到DOM的内容。

Hope this helps! 希望这可以帮助! :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM