繁体   English   中英

Twitter按钮不显示Tweet的URL

[英]Twitter button not showing URL to Tweet

我正在尝试在我的wordpress网站上实现一个“推文”按钮,我遇到的问题是网址没有显示在推文中。

它显示主题标签和帐户,但不显示URL。

我的代码如下:

<a href="https://twitter.com/share" class="twitter-share-button" data-url="<?php echo urlencode(get_permalink($post->ID)); ?>" data-via="mixtapes_4_free" data-related="realdannys" data-hashtags="mixtape">Tweet</a>

如果我不使用data-url那么它只是插入基本域名而不是完整的URL。

有任何想法吗?

您是否尝试过Twitter页面上的代码? 这里看一下twitter API。

你缺少javascript代码。

你可能从这里得到了你的代码

对于共享链接,javascript代码是

<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script>

只需在锚标记后添加此代码即可。

我有以下代码:

<a href="<?php echo $tweet_url; ?>" class="twitter-share-button" data-url="=<?php the_permalink(); ?>" data-via="<?php echo of_get_option( 'ac_twitter_username' ); ?>" data-lang="en" data-related="anywhereTheJavascriptAPI" data-count="vertical">Tweet</a>
<script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0],p=/^http:/.test(d.location)?'http':'https';if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src=p+'://platform.twitter.com/widgets.js';fjs.parentNode.insertBefore(js,fjs);}}(document, 'script', 'twitter-wjs');</script>

但这没用,所以我试了一个简化版:

<a href="http://twitter.com/share?url=<?php echo urlencode(wp_get_shortlink()); ?>&amp;counturl=<?php urlencode(the_permalink()); ?>" class="twitter-share-button" data-count="vertical" data-via="<?php echo of_get_option( 'ac_twitter_username' ); ?>">Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>

这个工作得很好。 请记住,用户名必须以某种方式集成到您的主题设置中,如果不是,您可以更改:

<?php echo of_get_option( 'ac_twitter_username' ); ?>

对于您的Twitter用户名。

我希望这有效,我花了一段时间才找到这种更通用的方法。 我不完全清楚我在做什么,所以如果有人发现这种方法的问题,请告诉我。 到目前为止,它运作良好。

谢谢!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM