4 twitter wordpress post ampersand
我希望得到一些帮助解决一些令我疯狂的编码问题.我最好在我的wordpress帖子标题中写"&"而不是"和".但写出&符号会破坏我们在twitter,facebook和google-plus上的帖子分享链接.Facebook能够实际显示链接(虽然它取出了标题中的&符号),但是twitter完全失败了,而google-plus也是如此.
这是共享链接的代码:
<ul>
<li class="video-twitter"><a target="_blank" href="http://twitter.com/share?text=<?php the_title(); ?>&url=<?php the_permalink(); ?>" title="Share on Twitter">Twitter</a></li>
<li class="video-facebook"><a target="_blank" href="http://www.facebook.com/sharer.php?u=<?php the_permalink();?>&t=<?php the_title(); ?>" title="Share on Facebook">Facebook</a></li>
<li class="video-google"><a target="_blank" href="https://plus.google.com/share?url=<?php the_permalink();?>&t=<?php the_title(); ?>" title="Share on Google+">Google+</a></li>
</ul>
Run Code Online (Sandbox Code Playgroud)
任何帮助将不胜感激!
我今天遇到了同样的问题而且很容易修复.所有&符号都由WordPress作为实体提供,这意味着如果你使用get_the_title()或the_title()&符号是这样的:&
您可以使用html_entity_decode()对此进行解码,然后您必须使其对URL友好,这可以使用urlencode()完成.
结合它们你会得到这个:
<?php print urlencode( html_entity_decode( get_the_title() ) ); ?>
Run Code Online (Sandbox Code Playgroud)
或者以"更清洁"的方式执行并在functions.php主题文件中创建此函数:
function themeprefix_social_title( $title ) {
$title = html_entity_decode( $title );
$title = urlencode( $title );
return $title;
}
Run Code Online (Sandbox Code Playgroud)
这样您就可以在主题中调用此函数,该函数可用于所有社交网络:
<?php print themeprefix_social_title( get_the_title() ); ?>
Run Code Online (Sandbox Code Playgroud)
应用于您的示例时:
<ul>
<li class="video-twitter"><a target="_blank" href="http://twitter.com/share?text=<?php print themeprefix_social_title( get_the_title() ); ?>&url=<?php the_permalink(); ?>" title="Share on Twitter">Twitter</a></li>
<li class="video-facebook"><a target="_blank" href="http://www.facebook.com/sharer.php?u=<?php the_permalink();?>&t=<?php print themeprefix_social_title( get_the_title() ); ?>" title="Share on Facebook">Facebook</a></li>
<li class="video-google"><a target="_blank" href="https://plus.google.com/share?url=<?php the_permalink();?>&t=<?php print themeprefix_social_title( get_the_title() ); ?>" title="Share on Google+">Google+</a></li>
</ul>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1819 次 |
最近记录: |