试图自定义Facebook分享按钮

时间:2013-11-27 04:24:32

标签: facebook wordpress

有办法吗?当点击Facebook分享按钮发布时,图片应该是帖子标题和描述的顶部,而不是留在Facebook共享窗口。

我已尝试将此代码用作Wordpress插件

add_image_size('fb-preview', 190, 190, true);
// get image preview
function ST4_get_FB_image($post_ID){
    $post_thumbnail_id = get_post_thumbnail_id( $post_ID );
    if ($post_thumbnail_id){
        $post_thumbnail_img = wp_get_attachment_image_src( $post_thumbnail_id, 'fb-preview');
        return $post_thumbnail_img[0];
    }
}
// get post description
function ST4_get_FB_description($post){
    if ($post->post_excerpt){
        return $post->post_excerpt;
    } else {
        // post excerpt is not set, we'll take first 55 words from post content
        $excerpt_length = 55;
        // clean post content
        $text = str_replace("\r\n"," ", strip_tags(strip_shortcodes($post->post_content)));
        $words = explode(' ', $text, $excerpt_length + 1);
        if (count($words) > $excerpt_length) {
            array_pop($words);
            $excerpt = implode(' ', $words);
            return $excerpt;
        }
    }
}

function ST4FB_header(){
    global $post;
    $post_featured_image = ST4_get_FB_image($post->ID);
    $post_description = ST4_get_FB_description($post);

    if ( (is_single()) AND ($post_featured_image) AND ($post_description) ){ 
?>  
    <link rel="image_src" href="<?php echo $post_featured_image; ?>" />
    <meta name="title" content="<?php echo $post->post_title; ?>" />
    <meta name="description" content="<?php echo $post_description; ?>" />

<?php
    }
}
add_action('wp_head','ST4FB_header');

和帖子页面上的Facebook分享按钮。

ID)); ?&GT;&amp; T公司= POST_TITLE); ?&gt;“&gt;在Facebook上分享enter image description here

1 个答案:

答案 0 :(得分:1)

我建议阅读facebook api docs并采取心态,他们不希望你弄乱他们的东西。

https://developers.facebook.com/docs/reference/dialogs/feed/

即使没有这种心态,这里的一切都表明你无法控制这一点,如果你这样做是一个未被发现的错误,fb会想要修复(因此破坏你发现的任何东西)。