替你的每一篇文章加上小貼紙

這是一篇把玩到一半的分享文章,有些內容講得不清不楚,有些地方需要你的補充~~

目的:
在你的每一篇文章(node,ex, blog)的最下頭,加上現在 web2.0 很流行的小貼紙。

必要條件:
Drupal 4.7
使用 phptemplate 的 theme
懂一點點 php

圖例:
請參考我剛剛這個文件的附加圖檔。

作法:
1. 修改你安裝的 theme 裏頭的 node.tpl.php
2. 決定你想把小貼紙放在哪,我是把它放在文章的最末端(links 的前頭)
3. 我決定加上 hemidemi, del.icio.us, digg 和 technorati 等的小貼紙,然後就前往各家 social bookmarks service 的官網,找到他們加小貼紙的 url。
4. 新增一個 stickers 的 css class 在你的 style.css。
5. 加上如下的程式碼就搞定了 :) (其中的 div="stickers" 這一段就是我加上去的)

in node.tpl.php

<?php if ($links): ?>
    <?php if ($picture): ?>
      <br class='clear' />
    <?php endif; ?>
    <div class="stickers">
        <a href="http://www.hemidemi.com/user_bookmark/new?title=<?php print $title ?>&url=<?php print $full_url ?>&description=<?php ?>&via=sticker">
            <img src="http://www.hemidemi.com/sticker/user/www.braverobbin.com.gif" title="Bookmark to HEMiDEMi" alt="Bookmark to HEMiDEMi" />
        </a>
        <a href="http://del.icio.us/post?url=<?php print $full_url ?>&title=<?php print $title ?>&tags=<?php ?>">
            <img src="http://www.ecogarden.net/deliciousicon.jpg" title="Keep this in Del.icio.us" alt="Keep this in Del.icio.us" />
        </a>
        <a href="http://digg.com/submit?phase=2&url=<?php print $full_url ?>&title=<?php print $title ?>&bodytext=<?php ?>&topic=<?php ?>">
            <img src="http://www.ecogarden.net/digg.gif" title="Digg it!" alt="Digg it!" />
        </a>
        <a href="http://www.technorati.com/faves?add=<?php print $full_url ?>" alt="Add to Technorati Favorites">
            <img src="http://www.technorati.com/favicon.ico" title="Add to Technorati Favorites" alt="Add to Technorati Favorites" />
        </a>
    </div>
    <div class="links"><?php print $links ?></div>
<?php endif; ?>

in style.css

.stickers {
        text-align: right;
}

in template.php
在 ooxx_node() 這個 function 裏頭(ooxx 代表你用的 theme 的名字),新增了一個 full_url 的變數給 theme,記得要加個 global $base_url; 在 ooxx_node() 的開頭,不然無法存取到。

'full_url'       => $base_url . url('node/'. $node->nid),

心得:
1. 使用了很直接的方法來得到 full_url,如果直接拿 node_url 這個變數的話,會得到相對路徑,這在站內的聯結是 ok 的,但要給別人時就不行了。
2. 我在想,好像改寫 theme_node 這個 function 時,可以 ooxx_node 也可以 phptemplate_node,還不是很清楚差別,即使看了官方的文件也還是搞不清楚。
3. drupal 官方的 handbook 雖然有很多寶,但有找東西還真是有點麻煩,尤其有時還得看完落落長的 comments 才會比較知道比較好的作法時,就覺得有點累人了~
4. 有不少貼紙的參數都不曉得要給啥值,像是 tag, description, topic...

參考資料:
Theme developer's guide
PHPTemplate Theme Snippets
Using Theme Override Functions
Themeable functions

附加檔案大小
drakeguan.org_with_stickers.png222 KB
DrakeGuan 的照片

warning!!

我 submit 這篇文章後,看到如下的 warning!!

warning: parse_url(/webmasters/tools/ping?sitemap=http://tw-drupal.info/gsitemap) [function.parse-url]: Unable to parse url in /home/.ashrei/twdrupal/tw-drupal.info/includes/common.inc on line 366.

Hipfox 的照片

Cool!!! 在 theme

Cool!!! 在 theme 中加連結,很直接。
那些參考資料看了真是令人頭暈,讀好幾遍才看出端倪呢~

DrakeGuan 的照片

後來從同事那得知,

後來從同事那得知,有個叫 service links 的 module 可以做到相同的事 :p
http://drupal.org/project/service_links