动态Facebook元标记(og:image,og:url) [英] Dynamic Facebook Meta Tags (og:image, og:url)

查看:333
本文介绍了动态Facebook元标记(og:image,og:url)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个问题。

我想在一个类似按钮的Facebook元标记中进行二次修改内容。
在其他线程中,我了解到,只要页面加载,我就不可能使用javascript或jquery atribute来更改元标记。

I want do dinamically change the content atribute in a facebook meta tag for a like button. 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.

我的想法是将facebok像按钮放在iframe中。在这个框架中,我想定义自己的元标记。这个元标记是由php $ _GET变量操纵的,这些变量是通过javascript来处理iframe内容的。

My idea is to put the facebok like button in a 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.

但它不起作用。

简单解释:
[index.php]

Simply explained: [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]

<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

推荐答案

你在正确的轨道上。只需为每个按钮提供唯一的网址。该URL可以具有相同的域,但是可以粘贴一些查询参数以使其唯一。

You're on the right track. Simply provide a unique URL for each like button. The URL can have the same domain, but tack on some query parameters to make it unique.

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

当用户点击类似时,将为与按钮相关联的绝对URL生成类似的内容。该协会通常在Facebook上生成一个故事。如果朋友决定点击该故事,他们将被重定向到相关联的URL。这就是您的元标记页面将会派上用场。

When a user clicks like, a like would be generated for the absolute URL associated with the button. The association usually generates a story on Facebook. If a friend decides to click on that story, they'll be redirected the the associated URL. That's where your meta tag page will come in handy.

为此,创建一个带有基本HTML的fb_meta.php页面,并放置在所需的任何开放图形标签中。有关开放式图形标签的更多信息,请参见此处这里。您可以使用您粘贴到网址上的查询参数(id)来动态生成开放图表标记的内容,使其独一无二。

For that, create a fb_meta.php page with basic HTML and drop in whatever open graph tags you need. 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.

简单的说,您可以提取该ID使用$ _GET ['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.

这是一个很好的想法是将简单的JavaScript重定向放回到您的主要网站或您喜欢的按钮的页面,以便用户不会卡在空白页面上。在您的fb_meta.php文件的body标签中,放置:

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. In the body tags of your fb_meta.php file, put:

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

这篇关于动态Facebook元标记(og:image,og:url)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆