PureNews

PureNews is an amazingly sleek and powerful news theme with unlimited color variations.

View full feature list Check out the live demo Buy this theme today

How to add the Digg button to selected articles only

Posted by on 19th Dec 2008 WordPress Coding & Design 8 comments

As you know, Digg is a powerful social website which can send more than 10,000 visitors to your blog if your article is pushed to the front page.

We can easily add the Digg button to our posts. All you need to do is simply copy the code and paste it into your template file and then the Digg button will appear in every post. But what if you only want the Digg button to appear in selected posts only?

How to make the Digg button appear in selected posts only

So, I am going to create a custom function and use custom fields to decide whether the Digg button will appear or not.

Open your template’s functions.php and paste the following codes before the ending tag ‘?>‘.

/* Return a Digg button */
function digg_button()
{
    global $post;

    $link=js_escape(get_permalink($post->ID));
    $title=js_escape($post->post_title);
    $text=js_escape(substr(strip_tags($post->post_content), 0, 350));        

    $button="
    <script type='text/javascript'>
    digg_url = '$link';
    digg_title = '$title';
    digg_skin = '$mode';
    digg_bodytext = '$text';
    </script>
    <script src='http://digg.com/tools/diggthis.js' type='text/javascript'></script>";

    if (get_post_meta($post->ID, 'digg-button', true) == 1)
        echo $button;
    else
        echo '';
}

Then, open your single.php, and add this code at the place where you want Digg button appear.

<?php digg_button(); ?>

So now that you have added the necessary code, simply add a custom field called “digg-button” with value 1 to the post that you want Digg button to appear.

Now, you can easily show or hide the Digg button using a custom field!

Feel free to add my Digg acount :)

I am the owner of www.bloghonour.com, which focus on discussing about blogging resources and also tutorials about how to customize your blog design. When i free, i write about internet, Web2.0, and introduce some application through my tech blog, www.techsnack.net

8 comments - Leave a reply
  • Posted by Christopher Ross on 19th Dec 2008

    Thanks, that's a great use of a custom function as well!

  • Posted by KevinMilton on 19th Dec 2008

    good one…………..thats perfect………..

  • Posted by Ajay on 20th Dec 2008

    Hi,

    Instead of a custom-field, I've created a plugin at http://ajaydsouza.com/wordpress/plugins/digg-inte… that integrates the javascript supplied by Digg themselves.

  • Posted by Nicholas Francis on 21st Dec 2008

    Really an good tip.. Thnks for sharing it with us

  • Posted by Blog Income Report on 21st Dec 2008

    Awesome, I've been looking for something like this. It's good to get some diggs but some posts just aren't suitable and it will be nice to be able to exclude them.

  • Posted by Dicky on 22nd Dec 2008

    Hi all,

    Thanks for reading my tutorials. I will try to improve it so that it can show different digg button based on your selection. Make sure you follow my feed or bloggingtips closely so that you won't miss this plugin!

  • Posted by love bebo skins on 28th Dec 2008

    great, always wanted to know how to do this, just bookmarked your site for fututre refrence.