WordPress建站教程:AddToAny Share Buttons插件分享文章到Facebook不显示标题的解决方法

前端之家收集整理的这篇文章主要介绍了WordPress建站教程:AddToAny Share Buttons插件分享文章到Facebook不显示标题的解决方法前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

我们在使用的过程中,经常需要给文章添加社交分享的功能。 是一款很不错的文章分享类型的wordpress插件,但是在使用的过程中,我们发现当把文章分享到Facebook的过程当中,并没有获取文章标题,而是将网站的链接分享到了Facebook。下面我们来看一下解决办法,解决的原理是:在wordpress网站的头部加入Open Graph Meta Info。

切换到主题目录,打开functions.PHP文件,加入以下代码

Crayon-5c891d21d744f387497987" class="Crayon-@R_502_156@ Crayon-theme-classic Crayon-font-monaco Crayon-os-pc print-yes notranslate" data-settings=" minimize scroll-mouSEOver" style=" margin-top: 12px; margin-bottom: 12px; font-size: 12px !important; line-height: 15px !important;">
Crayon-toolbar" data-settings=" mouSEOver overlay hide delay" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">Crayon-title">functions.PHP
Crayon-tools" style="font-size: 12px !important;height: 18px !important; line-height: 18px !important;">
Crayon-button Crayon-nums-button" title="Toggle Line Numbers">
Crayon-button-icon">
Crayon-button Crayon-plain-button" title="Toggle Plain Code">
Crayon-button-icon">
Crayon-button Crayon-wrap-button" title="Toggle Line Wrap">
Crayon-button-icon">
Crayon-button Crayon-expand-button" title="Expand Code">
Crayon-button-icon">
Crayon-button Crayon-copy-button" title="Copy">
Crayon-button-icon">
Crayon-button Crayon-popup-button" title="Open Code In New Window">
Crayon-button-icon">
Crayon-info" style="min-height: 16.8px !important; line-height: 16.8px !important;">
Crayon-plain-wrap">
Crayon-main" style="">Crayon-table">Crayon-row">
Crayon-nums " data-settings="show">
Crayon-nums-content" style="font-size: 12px !important; line-height: 15px !important;">
Crayon-num" data-line="Crayon-5c891d21d744f387497987-1">1
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d21d744f387497987-2">2
Crayon-num" data-line="Crayon-5c891d21d744f387497987-3">3
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d21d744f387497987-4">4
Crayon-num" data-line="Crayon-5c891d21d744f387497987-5">5
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d21d744f387497987-6">6
Crayon-num" data-line="Crayon-5c891d21d744f387497987-7">7
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d21d744f387497987-8">8
Crayon-num" data-line="Crayon-5c891d21d744f387497987-9">9
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d21d744f387497987-10">10
Crayon-num" data-line="Crayon-5c891d21d744f387497987-11">11
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d21d744f387497987-12">12
Crayon-num" data-line="Crayon-5c891d21d744f387497987-13">13
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d21d744f387497987-14">14
Crayon-num" data-line="Crayon-5c891d21d744f387497987-15">15
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d21d744f387497987-16">16
Crayon-num" data-line="Crayon-5c891d21d744f387497987-17">17
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d21d744f387497987-18">18
Crayon-num" data-line="Crayon-5c891d21d744f387497987-19">19
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d21d744f387497987-20">20
Crayon-num" data-line="Crayon-5c891d21d744f387497987-21">21
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d21d744f387497987-22">22
Crayon-num" data-line="Crayon-5c891d21d744f387497987-23">23
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d21d744f387497987-24">24
Crayon-num" data-line="Crayon-5c891d21d744f387497987-25">25
Crayon-num Crayon-striped-num" data-line="Crayon-5c891d21d744f387497987-26">26
Crayon-num" data-line="Crayon-5c891d21d744f387497987-27">27
Crayon-code">
Crayon-pre" style="font-size: 12px !important; line-height: 15px !important; -moz-tab-size:4; -o-tab-size:4; -webkit-tab-size:4; tab-size:4;">
Crayon-line" id="Crayon-5c891d21d744f387497987-1">Crayon-c">//Adding the Open Graph in the Language Attributes
Crayon-line Crayon-striped-line" id="Crayon-5c891d21d744f387497987-2">Crayon-t">functionCrayon-h"> Crayon-e">add_opengraph_doctypeCrayon-sy">(Crayon-h"> Crayon-sy">$Crayon-i">outputCrayon-h"> Crayon-sy">)Crayon-h"> Crayon-sy">{
Crayon-line" id="Crayon-5c891d21d744f387497987-3">Crayon-h"> Crayon-st">returnCrayon-h"> Crayon-sy">$Crayon-i">outputCrayon-h"> Crayon-sy">.Crayon-h"> Crayon-s">' xmlns:og="http://opengraPHProtocol.org/schema/" xmlns:fb="http://www.facebook.com/2008/fbml"'Crayon-sy">;
Crayon-line Crayon-striped-line" id="Crayon-5c891d21d744f387497987-4">Crayon-sy">}
Crayon-line" id="Crayon-5c891d21d744f387497987-5">Crayon-e">add_filterCrayon-sy">(Crayon-s">'language_attributes'Crayon-sy">,Crayon-h"> Crayon-s">'add_opengraph_doctype'Crayon-sy">)Crayon-sy">;
Crayon-line Crayon-striped-line" id="Crayon-5c891d21d744f387497987-6">Crayon-c">//Lets add Open Graph Meta Info
Crayon-line" id="Crayon-5c891d21d744f387497987-7">Crayon-t">functionCrayon-h"> Crayon-e">insert_fb_in_headCrayon-sy">(Crayon-sy">)Crayon-h"> Crayon-sy">{
Crayon-line Crayon-striped-line" id="Crayon-5c891d21d744f387497987-8">Crayon-h"> Crayon-m">globalCrayon-h"> Crayon-sy">$Crayon-v">postCrayon-sy">;
Crayon-line" id="Crayon-5c891d21d744f387497987-9">Crayon-h"> Crayon-st">ifCrayon-h"> Crayon-sy">(Crayon-h"> Crayon-o">!Crayon-e">is_singularCrayon-sy">(Crayon-sy">)Crayon-sy">)Crayon-h"> Crayon-c">//if it is not a post or a page
Crayon-line Crayon-striped-line" id="Crayon-5c891d21d744f387497987-10">Crayon-h"> Crayon-st">returnCrayon-sy">;
Crayon-line" id="Crayon-5c891d21d744f387497987-11">Crayon-h">Crayon-i">echoCrayon-h"> Crayon-s">'<Meta property="fb:admins" content="1721535331409376"/>'Crayon-sy">;
Crayon-line Crayon-striped-line" id="Crayon-5c891d21d744f387497987-12">Crayon-h">Crayon-i">echoCrayon-h"> Crayon-s">'<Meta property="og:title" content="'Crayon-h"> Crayon-sy">.Crayon-h"> Crayon-e">get_the_titleCrayon-sy">(Crayon-sy">)Crayon-h"> Crayon-sy">.Crayon-h"> Crayon-s">'"/>'Crayon-sy">;
Crayon-line" id="Crayon-5c891d21d744f387497987-13">Crayon-h">Crayon-i">echoCrayon-h"> Crayon-s">''Crayon-sy">;
Crayon-line Crayon-striped-line" id="Crayon-5c891d21d744f387497987-14">Crayon-h">Crayon-i">echoCrayon-h"> Crayon-s">'<Meta property="og:url" content="'Crayon-h"> Crayon-sy">.Crayon-h"> Crayon-e">get_permalinkCrayon-sy">(Crayon-sy">)Crayon-h"> Crayon-sy">.Crayon-h"> Crayon-s">'"/>'Crayon-sy">;
Crayon-line" id="Crayon-5c891d21d744f387497987-15">Crayon-h">Crayon-i">echoCrayon-h"> Crayon-s">'<Meta property="og:site_name" content="Eris"/>'Crayon-sy">;
Crayon-line Crayon-striped-line" id="Crayon-5c891d21d744f387497987-16">Crayon-h"> Crayon-st">ifCrayon-sy">(Crayon-o">!Crayon-e">has_post_thumbnailCrayon-sy">(Crayon-h"> Crayon-sy">$Crayon-v">postCrayon-i">IDCrayon-h"> Crayon-sy">)Crayon-sy">)Crayon-h"> Crayon-sy">{Crayon-h"> Crayon-c">//the post does not have featured image,use a default image
Crayon-line" id="Crayon-5c891d21d744f387497987-17">Crayon-h"> Crayon-sy">$Crayon-v">default_imageCrayon-o">=Crayon-s">"https://cdn.91wordpress.com/wp-content/uploads/2015/07/logo.png"Crayon-sy">;Crayon-h"> Crayon-c">//replace this with a default image on your server or an image in your media library
Crayon-line Crayon-striped-line" id="Crayon-5c891d21d744f387497987-18">Crayon-h"> Crayon-i">echoCrayon-h"> Crayon-s">'<Meta property="og:image" content="'Crayon-h"> Crayon-sy">.Crayon-h"> Crayon-sy">$Crayon-v">defaultCrayon-sy">_imageCrayon-h"> Crayon-sy">.Crayon-h"> Crayon-s">'"/>'Crayon-sy">;
Crayon-line" id="Crayon-5c891d21d744f387497987-19">Crayon-h"> Crayon-sy">}
Crayon-line Crayon-striped-line" id="Crayon-5c891d21d744f387497987-20">Crayon-h"> Crayon-st">elseCrayon-sy">{
Crayon-line" id="Crayon-5c891d21d744f387497987-21">Crayon-h"> Crayon-sy">$Crayon-v">thumbnail_srcCrayon-h"> Crayon-o">=Crayon-h"> Crayon-e">wp_get_attachment_image_srcCrayon-sy">(Crayon-h"> Crayon-e">get_post_thumbnail_idCrayon-sy">(Crayon-h"> Crayon-sy">$Crayon-v">postCrayon-i">IDCrayon-h"> Crayon-sy">)Crayon-sy">,Crayon-h"> Crayon-s">'medium'Crayon-h"> Crayon-sy">)Crayon-sy">;
Crayon-line Crayon-striped-line" id="Crayon-5c891d21d744f387497987-22">Crayon-h"> Crayon-i">echoCrayon-h"> Crayon-s">'<Meta property="og:image" content="'Crayon-h"> Crayon-sy">.Crayon-h"> Crayon-e">esc_attrCrayon-sy">(Crayon-h"> Crayon-sy">$Crayon-v">thumbnail_srcCrayon-sy">[Crayon-cn">0Crayon-sy">]Crayon-h"> Crayon-sy">)Crayon-h"> Crayon-sy">.Crayon-h"> Crayon-s">'"/>'Crayon-sy">;
Crayon-line" id="Crayon-5c891d21d744f387497987-23">Crayon-h"> Crayon-sy">}
Crayon-line Crayon-striped-line" id="Crayon-5c891d21d744f387497987-24">Crayon-h"> Crayon-i">echoCrayon-h"> Crayon-s">"
Crayon-line" id="Crayon-5c891d21d744f387497987-25">Crayon-s">"Crayon-sy">;
Crayon-line Crayon-striped-line" id="Crayon-5c891d21d744f387497987-26">Crayon-sy">}
Crayon-line" id="Crayon-5c891d21d744f387497987-27">Crayon-e">add_actionCrayon-sy">(Crayon-h"> Crayon-s">'wp_head'Crayon-sy">,Crayon-h"> Crayon-s">'insert_fb_in_head'Crayon-sy">,Crayon-h"> Crayon-cn">5Crayon-h"> Crayon-sy">)Crayon-sy">;

上面的代码中需要添加您自己的facebook的app_id,申请的链接如下:

添加完成以后您可以通过下面的链接进行测试,Debugger

猜你在找的wordpress相关文章