Wordpress PHP中的动态Facebook og元标记 [英] Dynamic Facebook og Meta Tags in Wordpress PHP

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

问题描述

我正在尝试添加动态Facebook og元标记到我的Wordpress网站。我将它们添加到single.php而不是通常推荐的functions.php文件,因为我已经创建了一个Facebook应用程序的代码,需要执行每次有人查看个人博客文章,因为它发布到他们的Facebook他们已经阅读了这篇文章的时间表。我不想使用插件,因为我的一些插件曾经相互冲突,这是一个很糟糕的事情。我最大的问题是,我需要 og:url 标签是动态的,虽然 og:title og:description og:image 等应该是一样的。以下是我的single.php文件顶部的代码:

I am trying to add dynamic Facebook og meta tags to my Wordpress site. I am adding them to single.php instead of the usually recommended functions.php file because I have code below that for a Facebook app I've created that needs to execute every time someone views an individual blog post because it then posts to their Facebook timeline that they've read that particular post. I don't want to use a plugin because some of my plugins used to conflict with each other and it was a mess to get that straightened out. My biggest problem is that I need the og:url tag to be dynamic, though the og:title, og:description, og:image, etc. should be as well. Here is the code I have at the top of my single.php file:

编辑:这里是我现在使用的工作代码。谢谢每个人的帮助:

HERE IS THE WORKING CODE I AM NOW USING. THANKS FOR EVERYONE'S HELP:

    <?php

$params = array();
if(count($_GET) > 0) {
    $params = $_GET;
} else {
    $params = $_POST;
}
// defaults
if($params['type'] == "") $params['type'] = "picture";
if($params['locale'] == "") $params['locale'] = "en_US";
if($params['description'] == "") $params['description'] = "Visit Internet LOLs for the funniest humor on the web! :)";
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">
    <head prefix="og: http://ogp.me/ns# fb: http://ogp.me/ns/fb# internetlolsapp: http://ogp.me/ns/fb/internetlolsapp#">
        <title></title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>

        <!-- Open Graph meta tags -->
        <meta property="fb:app_id" content="378076268920252" />
        <meta property="og:site_name" content="meta site name"/>
        <meta property="og:url" content="<?php echo 'http://internetlols.com'.$_SERVER['REQUEST_URI']; ?>"/>
        <meta property="og:type" content="internetlolsapp:<?php echo $params['type']; ?>"/>

        <meta property="og:description" content="<?php echo $params['description']; ?>"/>

    </head>
</html>

  <script type="text/javascript">
  function postView()
  {
      FB.api(
        '/me/internetlolsapp:view',
        'post',
        { picture: '<?php echo 'http://internetlols.com'.$_SERVER['REQUEST_URI']; ?>' },
        function(response) {
       if (!response) {
          // FAIL GRACEFULLY alert('Error occurred : No Response');
       } else if (response.error) {
          // FAIL GRACEFULLY alert('Error occurred : ' + response.error);
       } else {
          // SUCCESS alert('View was successful! Action ID: ' + response.id);
       }
        });
  }
  </script>
</head>
<body>
  <div id="fb-root"></div>
  <script>
    window.fbAsyncInit = function() {
      FB.init({
        appId      : '378076268920252', // App ID
        status     : true, // check login status
        cookie     : true, // enable cookies to allow the server to access the session
        xfbml      : true  // parse XFBML
      });
    };

    // Load the SDK Asynchronously
    (function(d){
      var js, id = 'facebook-jssdk'; if (d.getElementById(id)) {return;}
      js = d.createElement('script'); js.id = id; js.async = true;
      js.src = "//connect.facebook.net/en_US/all.js";
      d.getElementsByTagName('head')[0].appendChild(js);
    }(document));
  </script>



</body>

<body onload='postView()'>
</html>

我试图按照这里的代码:动态生成Facebook Open Graph元标记
,它会发布到我的Facebook时间线,每当我阅读博客帖子,但是标题当然会发布默认标题,当我点击我的Facebook时间轴上的默认标题链接时,它会发送到一个无关的单一的网址

I am trying to follow the code located here: Generating Facebook Open Graph meta tags dynamically and it DOES post to my Facebook timeline whenever I read a blog post, but for the title it of course posts "default title" and when I click the "default title" link on my Facebook timeline, it sends me to the URL for single.php with a bunch of nonsense at the end of the URL

http://MYSITE.com/wp-content/themes/twentyeleven/single.php?fb_action_ids=10151048340001514&fb_action_types=internetlolsapp%3Aview&fb_source=other_multiline

而不是博客文章URL。我想知道如果它与FB.api之后的第三行中的URL有任何关系,但是我尝试过的其他任何东西阻止了当我阅读时在我的Facebook时间轴上发布任何内容一个博文。

instead of the blog post URL. I'm wondering if it has anything to do with the URL I put in the 3rd line after "FB.api", but anything else I've tried putting there prevents the app from posting anything at all on my Facebook timeline when I read a blog post.

任何想法如何解决这个问题?我已经把我的头发拉了几天了。任何帮助将不胜感激!感谢提前。

Any ideas how to fix this? I've been pulling my hair out for days with this. Any help would be most appreciated! Thanks in advance.

推荐答案

我从Facebook特色图像和开放图表元标记( http://www.ryanscowles.com )并将其粘贴到functions.php上,它可以工作!
(wordpress 3.5.1)

I adapted a function from Facebook Featured Image and Open Graph Meta Tags(http://www.ryanscowles.com) and pasted it on functions.php, it works! (wordpress 3.5.1)

<?php
//function to limit description to 300 characters
function limit($var, $limit) {
    if ( strlen($var) > $limit ) {
        return substr($var, 0, $limit) . '...';
    }
    else {
        return $var;
    }
}

// Set your Open Graph Meta Tags
function fbogmeta_header() {
    if (is_single()) {
        //getting the right post content
        $postsubtitrare = get_post_meta($post->ID, 'id-subtitrare', true);
        $post_subtitrare = get_post($postsubtitrare);
        $content = limit(strip_tags($post_subtitrare-> post_content),297);
        ?>
        <meta property="og:title" content="<?php the_title(); ?>"/>
        <meta property="og:description" content="<?php echo $content; ?>" />
        <meta property="og:url" content="<?php the_permalink(); ?>"/>
        <?php $fb_image = wp_get_attachment_image_src(get_post_thumbnail_id(     get_the_ID() ), 'thumbnail'); ?>
        <?php if ($fb_image) : ?>
        <meta property="og:image" content="<?php echo $fb_image[0]; ?>" />
        <?php endif; ?>
        <meta property="og:type" content="<?php
        if (is_single() || is_page()) { echo "article"; } else { echo "website";}     ?>"
        />
        <meta property="og:site_name" content="<?php bloginfo('name'); ?>"/>
        <?php
        }
        }
add_action('wp_head', 'fbogmeta_header');
?>

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

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