wordpress熱門標(biāo)簽添加小圖標(biāo)的方法

字號:


    wordpress分類可以加個(gè)性圖標(biāo),那么標(biāo)簽是不是也可以加上圖標(biāo)?當(dāng)然可以!官網(wǎng)codex已為我們提供了現(xiàn)成的代碼:http://codex.wordpress.org/function_reference/get_the_tags
    <?php
    $posttags = get_the_tags();
    if ($posttags) {
    foreach($posttags as $tag) {
    echo '<img src=http://example.com/images/' . $tag->term_id . '.jpg
    alt=' . $tag->name . ' />';
    }
    }
    ?>
    官網(wǎng)原代碼中的標(biāo)簽無鏈接,小圖標(biāo)地址是絕對路徑,簡單修改一下:
    <?php
    $posttags = get_the_tags();
    if ($posttags) {
    foreach($posttags as $tag) {
    echo '<a href='.get_tag_link($tag).'><img src= '.get_bloginfo('template_directory').'/image/' . $tag->term_id . '.jpg />'. $tag->name .'</a>';
    }
    }
    ?>
    小圖標(biāo)地址改為相對路徑,可以將小圖標(biāo)放到當(dāng)前主題 image 目錄中,默認(rèn)小圖標(biāo)名稱以相應(yīng)的標(biāo)簽id命名。
    可以修改其中的:
    $tag->term_id為$tag->name
    這樣圖標(biāo)可以直接用標(biāo)簽名稱命名。
    用上述代碼替換主題默認(rèn)的標(biāo)簽函數(shù):
    <?php the_tags(); ?>
    如果主題無類似的標(biāo)簽調(diào)用函數(shù),可以加到模板主循環(huán)適當(dāng)?shù)奈恢谩?BR>    最后,用css樣式美化一下,即可。