Facebook预览Open Graph Object Debugger

时间:2015-03-24 作者:skifast

如果我试图在Facebook上公开我的网站,我的特色图片会出错。

我尝试使用facebook debug 但我没有任何感兴趣的信息。

我使用此函数,但无法解决:

//* FACEBOOK *//

function insert_fb_in_head() {
    global $post;
    if ( !is_singular()) // Se non è un post o una pagina
        return;

        $mytext=$post->post_excerpt;
        $myfulltext=strip_tags($post->post_content);
        if(strlen($mytext) > 250) $mytext = substr($mytext, 0, 250).\'...\';
        if(strlen($myfulltext) > 250) $myfulltext = substr($myfulltext, 0, 250).\'...\';
        if(empty($post->post_excerpt)) {
        $mytext=$myfulltext;
        }

        $mydesc=$mytext;
    $external_posts=$post->ID;

        echo \'<meta property="fb:admins" content="IDPAGE" />\';
        echo "\\n";
        echo \'<meta property="og:title" content="\' . get_the_title() . \'" />\';
        echo "\\n";
        echo \'<meta property="og:type" content="article" />\';
        echo "\\n";
        echo \'<meta property="og:description" content="\' . $mydesc . \'" />\';
        echo "\\n";
        echo \'<meta property="og:url" content="\' . get_permalink() . \'" />\';
        echo "\\n";
        echo \'<meta property="og:site_name" content="TITLE WEBSITE" />\';
        echo "\\n";
        echo \'<meta property="og:locale" content="it_IT" />\';
        echo "\\n";

        $myurl = wp_get_attachment_image_src( get_post_thumbnail_id($external_posts), \'thumbnail\' );
        $postimage=$myurl[0];

        if(empty($postimage)) {
        $default_image="URLIMAGE";
        echo \'<meta property="og:image" content="\' . $default_image . \'" />\';
        }
        else {
        $default_image=$postimage;
        echo \'<meta property="og:image" content="\' . $default_image . \'" />\';
        }
        echo "\\n"; echo "\\n";

}

add_action( \'wp_head\', \'insert_fb_in_head\', 1 );
所以我不知道如何设置特定的图像。谢谢

1 个回复
最合适的回答,由SO网友:jimihenrik 整理而成

试试这个:在你的函数中。php将此

function fb_excerpt($text, $excerpt) {
    if ($excerpt) return $excerpt;

    $raw_excerpt = $text;

    $text = strip_shortcodes( $text );

    $text = apply_filters(\'the_content\', $text);
    $text = str_replace(\']]>\', \']]&gt;\', $text);
    $text = strip_tags($text);
    $excerpt_length = apply_filters(\'excerpt_length\', 55);
    $words = preg_split("/[\\n\\r\\t ]+/", $text, $excerpt_length + 1, PREG_SPLIT_NO_EMPTY);
    if (count($words) > $excerpt_length) {
            array_pop($words);
            $text = implode(\' \', $words);
            $text = $text . \'...\';
    } else { $text = implode(\' \', $words); }

    return apply_filters(\'wp_trim_excerpt\', $text, $raw_excerpt);
}
在你的标题中。你们之间的php<head></head> 标签放置此

<?php global $post;
if (is_singular()) { ?>
    <meta property="fb:admins" content="522774907844630">
    <meta property="og:title" content="<?php the_title(); ?>">
    <meta property="og:description" content="<?php echo fb_excerpt($post->post_content, get_the_excerpt()); ?>">
    <meta property="og:type" content="article">
    <meta property="og:image" content="<?php echo wp_get_attachment_url(get_post_thumbnail_id($post->ID)); ?>">
    <meta property="og:site_name" content="<?php bloginfo(\'name\'); ?>">
    <meta property="og:url" content="<?php echo(\'http://\'.$_SERVER[\'HTTP_HOST\'].$_SERVER[\'REQUEST_URI\']); ?>">
<?php } ?>

结束

相关推荐

在WordPress中使用自定义标题、URL和图像的自定义Facebook共享按钮

在我的所有帖子页面中,有共享按钮(通过AddThis plugin).当我在Facebook上单击共享时,它会显示URL和标题,但图像会显示其他内容。For example.我试图在下面创建一个自定义共享按钮,但当我单击它时,什么也没有发生。 <?php $title = the_title(); $url= the_permalink(); $summary=urlencode(\'Custom message that summarizes wh