简体   繁体   English

动态Facebook Meta标签(og:image,og:url)

[英]Dynamic Facebook Meta Tags (og:image, og:url)

I got a problem. 我有问题

I want do dinamically change the content atribute in a facebook meta tag for a like button. 我想从动力学上更改一个类似按钮的facebook meta标签中的内容属性。 In other threads i have learned that ist's not possible to change the meta tag with javascript or jquery atribute as soon as the page is loaded. 在其他线程中,我了解到,一旦页面加载完毕,ist就无法使用javascript或jquery属性更改meta标签。

My idea is to put the facebok like button in a iframe. 我的想法是将Facebok像按钮放在iframe中。 In this frame i want to define own meta tags. 在此框架中,我想定义自己的元标记。 This meta tags were manipulatet by php $_GET variables which are sendt over javascript who manipulate the iframe content. 该元标记由php $ _GET变量操纵,这些变量通过操纵iframe内容的javascript发送。

But it does not work.. 但这不起作用..

Simply explained: [index.php] 简单解释一下:[index.php]

<html>
 <head>
   <title>RandomPage</title>
 </head>
 <body>
  <iframe id="fb" src="fb.php">
  <iframe>
  <input type="submit" onclick="changelikebutton();" />
  <script type="text/JavaScript">
   function changelikebutton(){
     document.getElementById('fb').src = 'fb.php?l=5';
   }
  <script>
 </body>
<html>

[fb.php] [fb.php]

<html>
 <head>
  <meta property="og:image" content="RandomPage.com/<?php echo $_GET['l']; ?>.jpg" >
 </head>
 <body>
  <facebook script code und id >
  <div id="code für fb like button">
 </body>
<html>

It's not the original code but it should explain my idea 这不是原始代码,但应该可以解释我的想法

You're on the right track. 您走在正确的轨道上。 Simply provide a unique URL for each like button. 只需为每个喜欢的按钮提供唯一的URL。 The URL can have the same domain, but tack on some query parameters to make it unique. 该URL可以具有相同的域,但是要附加一些查询参数以使其唯一。

<div class="fb-like" data-href="http://yourdomain.com/fb_meta.php?id=1" data-send="false" data-width="450" data-show-faces="true"></div>

When a user clicks like, a like would be generated for the absolute URL associated with the button. 当用户单击“赞”时,将为与该按钮关联的绝对URL生成一个赞。 The association usually generates a story on Facebook. 该协会通常会在Facebook上产生一个故事。 If a friend decides to click on that story, they'll be redirected the the associated URL. 如果朋友决定点击该故事,他们将被重定向到相关的URL。 That's where your meta tag page will come in handy. 这就是您的元标记页面派上用场的地方。

For that, create a fb_meta.php page with basic HTML and drop in whatever open graph tags you need. 为此,使用基本HTML创建一个fb_meta.php页面,并放入您需要的任何打开的图形标记。 More info on open graph tags can be found here and here . 有关开放图标记的更多信息,请参见此处此处 You can dynamically generate the content of your open graph tags using the query parameter (id) that you tacked onto your URL to make it unique. 您可以使用添加到URL上的查询参数(id)动态生成打开的图形标签的内容,以使其唯一。

Simply put, you could extract the id using $_GET['id'] and then do a call on your database, array, or whatever to get the content for the meta tag. 简而言之,您可以使用$ _GET ['id']提取ID,然后对您的数据库,数组或任何其他方法进行调用,以获取meta标签的内容。

It's a good idea to drop in a simple javascript redirect back to your main site, or page where your like button is so that users don't get stuck on a blank page. 最好将简单的javascript重定向放回到您的主站点或您的“喜欢”按钮所在的页面,以免用户停留在空白页面上。 In the body tags of your fb_meta.php file, put: 在fb_meta.php文件的body标签中,输入:

<script>window.location = 'http://yourdomain.com';</script>

I believe just changing the src attribute is not enough. 我相信仅更改src属性是不够的。 I think you should reload the iframe content. 我认为您应该重新加载iframe内容。 Here is a nice solution for this: Dynamically change iframe's content 这是一个不错的解决方案: 动态更改iframe的内容

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

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