DickyHow to add the Digg button to selected articles only

Written by Dicky from BlogHonour

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 :)

Dicky Written by Dicky from BlogHonour
Posted on December 19th, 2008 and filed under WordPress Coding & Design
Do not forget to subscribe to our RSS feed for updates
  • Digg This Post
  • Tweet This Post
  • Stumble This Post
  • Submit This Post To Delicious
  • Submit This Post To Reddit
  • Submit This Post To Mixx
  • BloggingTips Uses Aweber

8 Responses to “How to add the Digg button to selected articles only”

Author comments are in a darker gray color for you to easily identify the posts author in the comments

  1. Thanks, that’s a great use of a custom function as well!

  2. KevinMilton says:

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

  3. Ajay says:

    Hi,

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

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

  5. 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.

  6. Dicky says:

    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!

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

Trackbacks

Comments are closed.

Comments are closed since this post is older than 30 days. However, you can continue this discussion in our popular Blogging Forums

Subscribe To BloggingTips Via RSS Subscribe To Blogging Tips Via Email Follow Us On Twitter Follow us on Facebook Find Out More About Our Newsletter

Sponsors

Blogging Tips Newsletter

Our WordPress Themes

 

Our Free E-Books