Search code examples
javascriptfacebooktwitter

Showing multiple twitter share buttons?


On a certain page in my site, a user has a list of items, like this:

<div id="item1">.....</div>
<div id="item2">.....</div>
<div id="item3">.....</div>

Each of these divs has a special link of its own on my site, like:

example.com/items/1
example.com/items/2
example.com/items/3

I want to show a twitter share button in each individual div, which when clicked, will let the user tweet the url for that particular item to their twitter.

Is there a plugin which does this?


Solution

  • There is no need for third party plugins. Go with the Twitter plugin:

    <!DOCTYPE html>
    <html lang="de">
      <head>
        <!-- meta -->
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <meta name="robots" content="noindex, nofollow">
        <title>Twitter Share Buttons</title>
      </head>
      <body>
        <div id="item1"><a href="https://twitter.com/share" class="twitter-share-button" data-url="http://example.com/items/1" data-text="Text to share 1">Tweet</a></div>
        <div id="item2"><a href="https://twitter.com/share" class="twitter-share-button" data-url="http://example.com/items/2" data-text="Text to share 2">Tweet</a></div>
        <div id="item3"><a href="https://twitter.com/share" class="twitter-share-button" data-url="http://example.com/items/3" data-text="Text to share 3">Tweet</a></div>
        <script>!function(d,s,id){var js,fjs=d.getElementsByTagName(s)[0];if(!d.getElementById(id)){js=d.createElement(s);js.id=id;js.src="//platform.twitter.com/widgets.js";fjs.parentNode.insertBefore(js,fjs);}}(document,"script","twitter-wjs");</script>
      </body>
    </html>