[英]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()); ?>&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.