Display Tag (automatically) as Hashtag In Twitter Share

twitter has built a code to display tweet button
<a href="https://twitter.com/share" class="twitter-share-button">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>
That code is not wordpress stylenya. so if you want to make it work for wordpress, you need to modify that code to be:
data-text='<?php the_title(); ?>'

above code display title with link that will  share url to twitter.  so the code would be:
<a href="http://twitter.com/share" class="twitter-share-button" data-text='<?php the_title(); ?>'>Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>

How to Display Tag in Tweet?
Next, how to convert Tag to Hashtag for twitter:  the main principle is

display wp tag  as plain text, add # sign, and shard it to twitter.

In wordpress, to display tag by syntax:
<?php the_tags(); ?>

see at  http://codex.wordpress.org/Function_Reference/the_tags
the problem is, the_tags() syntax comes with hyperlink. Meanwhile we just need to display plain text. so just change the code to be:
<?php echo strip_tags(get_the_tag_list('',',','')); ?>

above code will display tag without link

TO Display #  sign
TO Display #  sign just typ0e &#35; and place it without space to tag that we`ve changed itn to plain text.

FINAL CODE
This code will display tag as hashtag in twitter share automatically.


<a href="http://twitter.com/share" class="twitter-share-button" data-text='<?php the_title(); ?> &#35;<?php echo strip_tags(get_the_tag_list('',',','')); ?>' data-count="vertical" >Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>

 

a Bonus :P
<a href="http://twitter.com/share" class="twitter-share-button" data-text='?<?php the_title(); ?>? «&#35;<?php echo strip_tags(get_the_tag_list('',',','')); ?>»' data-count="vertical" >Tweet</a><script type="text/javascript" src="http://platform.twitter.com/widgets.js"></script>

 Screenshot

Share on Google Plus

About JackSparrow

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

0 komentar:

Post a Comment