如何像 WordPress 博客页面一样为每个 facebook 设置图像、描述和标题?

发布于 2024-12-05 07:22:17 字数 355 浏览 1 评论 0原文

可能的重复:
Facebook Sharer 如何选择图像?

所以我想设置一个不同的当用户在博客主页上点击某篇文章的“赞”时,每篇文章的图像、标题和描述。这与 Mashable 和 Techcrunch 的做法相同。到目前为止,我只能找到包括在头部添加元标记的解决方案。但问题是主页有很多帖子,每个帖子都有不同的标题、图片等。那么我该怎么做,有人有任何想法吗?

Possible Duplicate:
How does Facebook Sharer select Images?

So I wanted to set a different image, title and description to each post when a user clicks like on that certain post on the main blog page. This is the same way that Mashable and Techcrunch have it. So far I've only been able to find solution that include adding a meta tag on the head section. But the problem is the home page has many posts and each post has a different title, image, etc. So do I do this, anyone has any idea?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

悲歌长辞 2024-12-12 07:22:17

我猜你必须为每个手动设置类似的东西。

http://developers.facebook.com/docs/reference/javascript/FB。 ui/

并且对于每个项目执行类似

<div id="post-id">
    <h2>title</h2>
    <img class="thumb" src="src.jpg">
    <p class="excerpt">the description</p>
    <a class="permalink" href="permalink">Read more</a>
    <a href="#" class="share">
</div>

Jquery

$(".share").click( function(e) {
    e.preventDefault();

    FB.ui(
      {
        method: 'feed',
        name: $(this).parent().child('h2').html(),
        link: $(this).parent().child('.permalink').attr('href'),
        picture: $(this).parent().child('img').html(),
        caption: 'Reference Documentation',
        description: 'Dialogs provide a simple, consistent interface for applications to interface with users.'
      },
      function(response) {
        if (response && response.post_id) {
          alert('Post was published.');
        } else {
          alert('Post was not published.');
        }
      }
    );      
});

Now 的操作,这个 jQuery 是不正确的(我很确定),并且您还需要拥有 Facebook 应用程序和所有设置。但我认为这种事情会运作得很好。

或者找到一个插件,例如 addthis.com 或 facebook“like”插件,如果您插入永久链接,则可能会起作用。
http://developers.facebook.com/docs/reference/plugins/like/

I'm guessing you'd have to manually set up something like this for each.

http://developers.facebook.com/docs/reference/javascript/FB.ui/

And for each item do something like

<div id="post-id">
    <h2>title</h2>
    <img class="thumb" src="src.jpg">
    <p class="excerpt">the description</p>
    <a class="permalink" href="permalink">Read more</a>
    <a href="#" class="share">
</div>

Jquery

$(".share").click( function(e) {
    e.preventDefault();

    FB.ui(
      {
        method: 'feed',
        name: $(this).parent().child('h2').html(),
        link: $(this).parent().child('.permalink').attr('href'),
        picture: $(this).parent().child('img').html(),
        caption: 'Reference Documentation',
        description: 'Dialogs provide a simple, consistent interface for applications to interface with users.'
      },
      function(response) {
        if (response && response.post_id) {
          alert('Post was published.');
        } else {
          alert('Post was not published.');
        }
      }
    );      
});

Now, this jQuery isn't right (i'm pretty sure), and you'll need to have the Facebook app and all that set up as well. But I'm thinking something of this sort would work pretty well.

Or find a plugin, like addthis.com or the facebook "like" plugin might work if you insert the permalink.
http://developers.facebook.com/docs/reference/plugins/like/

谈场末日恋爱 2024-12-12 07:22:17

您可以编写一个函数,获取帖子图片、标题和描述,并将其动态插入到该部分中:

function insert_fb_in_head() 
{

        if (is_singular()) { 
            $image = get_the_post_thumbnail(); 
            $title = get_the_title(); 
            $type = 'article';
        }
        else { 
            $image = 'standard_image.jpg';
            $title = bloginfo('name');
            $type = 'blog';
        }
        echo '<meta property="og:title" content="'.$title.'"/>'; 
        echo '<meta property="og:type" content="'.$type.'"/>';
        echo '<meta property="og:url" content="'.get_permalink().'"/>'; 
        echo '<meta property="og:site_name" content="'.$title.'"/>'; 
        echo '<meta property="og:email" content="[email protected]"/>';
        echo '<meta property="og:phone_number" content="000 555 888"/>'; 
        echo '<meta property="og:description" content="'.meta_description().'"/>';
        echo '<meta property="og:image" content="' . $image . '"/>'; 
}
add_action( 'wp_head', 'insert_fb_in_head', 5 );

它:

function meta_description() 
{
    $description = "";
    if ( is_single() || is_page() ) 
    {
        $post_id = get_the_ID();
        $description = get_post_meta($post_id, 'description', true);
    } 
    else { $description = get_bloginfo('name')." - ".get_bloginfo('description'); }
    return $description;
}

...为了获取描述,我刚刚在我的帖子中添加了一个“自定义字段”,并在此处使用此函数读取 在这里定义标题、图像和描述是更好的方法,但我认为这可以让你知道如何做到这一点。至少我对这样的方式很满意。

PS:为了澄清这一点,根据您的需要更改变量并将其放入模板的functions.php 文件中,仅此而已。一旦有人点击您希望已插入博客中的“赞”按钮,标题、图像和描述就会出现在他/她的 Facebook 墙上!

You can write a function that takes the posts picture, title and description and dynamically inserts that into the Section:

function insert_fb_in_head() 
{

        if (is_singular()) { 
            $image = get_the_post_thumbnail(); 
            $title = get_the_title(); 
            $type = 'article';
        }
        else { 
            $image = 'standard_image.jpg';
            $title = bloginfo('name');
            $type = 'blog';
        }
        echo '<meta property="og:title" content="'.$title.'"/>'; 
        echo '<meta property="og:type" content="'.$type.'"/>';
        echo '<meta property="og:url" content="'.get_permalink().'"/>'; 
        echo '<meta property="og:site_name" content="'.$title.'"/>'; 
        echo '<meta property="og:email" content="[email protected]"/>';
        echo '<meta property="og:phone_number" content="000 555 888"/>'; 
        echo '<meta property="og:description" content="'.meta_description().'"/>';
        echo '<meta property="og:image" content="' . $image . '"/>'; 
}
add_action( 'wp_head', 'insert_fb_in_head', 5 );

...to get the description I just added a 'custom field' in my posts and read it with this function here:

function meta_description() 
{
    $description = "";
    if ( is_single() || is_page() ) 
    {
        $post_id = get_the_ID();
        $description = get_post_meta($post_id, 'description', true);
    } 
    else { $description = get_bloginfo('name')." - ".get_bloginfo('description'); }
    return $description;
}

There may be better ways to define the title, image and description in here, but that gives you an idea how to do it I think. At least I'm quite happy with that way.

PS: Just to clarify, change the variables to your needs and put this in your functions.php file of your template and that's pretty much it. As soon as someone clicks the like button you hopefully inserted already in your blog, the title, image and description will appear on his/her facebook wall!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文