Typecho 108 4

    Typecho根据slug添加icon

    使用穷举的方式来匹配自定义icon

    根据分类的slug来匹配

                                <?php 
                                switch($categories->slug) {
                                    case 'images': echo '<i class="bi bi-images me-1"></i>';
                                break;
                                    case 'share': echo '<i class="bi bi-share-fill me-1"></i>';
                                break;
                                    case 'NULL': echo '<i class="bi bi-speaker-fill me-1"></i>';
                                break;
                                    case 'memos': echo '<i class="bi bi-chat me-1"></i>';
                                break;
                                    case 'codes': echo '<i class="bi bi-code me-1"></i>';
                                break;
                                    case 'logs': echo '<i class="bi bi-person-fill me-1"></i>';
                                break;
                                    case 'test': echo '<i class="bi bi-calendar-fill me-1"></i>';
                                break;
                                    case 'tools': echo '<i class="bi bi-tools me-1"></i>';
                                break;
                                    case 'music': echo '<i class="bi bi-music-note me-1"></i>';
                                break;
                                    case 'links': echo '<i class="bi bi-link me-1"></i>';
                                break;
                                    case 'video': echo '<i class="bi bi-camera-video me-1"></i>';
                                break;
                                    case 'life': echo '<i class="bi bi-heart-fill me-1"></i>';
                                break;
                                    case 'study': echo '<i class="bi bi-book-fill me-1"></i>';
                                break;
                                    case 'news': echo '<i class="bi bi-newspaper me-1"></i>';
                                break;
                                    case 'themes': echo '<i class="bi bi-palette me-1"></i>';
                                break;
                                    case 'plugins': echo '<i class="bi bi-gear-fill me-1"></i>';
                                break;
                                    case 'photo': echo '<i class="bi bi-images me-1"></i>';
                                break;
                                    default: echo '<i class="bi bi-folder-fill me-1"></i>';
                                } ?>
    

    同样也可以根据自定义页面的slug匹配

    <?php $pages = Typecho_Widget::widget('Widget_Contents_Page_List'); ?>
    <?php while($pages->next()): ?>
    <li>
        <a href="<?php $pages->permalink(); ?>">
        <?php 
        switch($pages->slug) {
            case 'about': echo '<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-user" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z"/><circle cx="12" cy="7" r="4" /><path d="M6 21v-2a4 4 0 0 1 4 -4h4a4 4 0 0 1 4 4v2" /></svg> '; // 关于页面
            break;
            case 'links': echo '<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-link" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z"/><path d="M10 14a3.5 3.5 0 0 0 5 0l4 -4a3.5 3.5 0 0 0 -5 -5l-.5 .5" /><path d="M14 10a3.5 3.5 0 0 0 -5 0l-4 4a3.5 3.5 0 0 0 5 5l.5 -.5" /></svg>'; // 链接页面
            break;
            case 'archives': echo '<svg xmlns="http://www.w3.org/2000/svg" class="icon icon-tabler icon-tabler-archive" width="24" height="24" viewBox="0 0 24 24" stroke-width="2" stroke="currentColor" fill="none" stroke-linecap="round" stroke-linejoin="round"><path stroke="none" d="M0 0h24v24H0z"/><rect x="3" y="4" width="18" height="4" rx="2" /><path d="M5 8v10a2 2 0 0 0 2 2h10a2 2 0 0 0 2 -2v-10" /><line x1="10" y1="12" x2="14" y2="12" /></svg>'; // 归档页面
            break;
            case 'gbook': echo '<svg  xmlns="http://www.w3.org/2000/svg"  class="icon icon-tabler icon-tabler-article" width="24"  height="24"  viewBox="0 0 24 24"  fill="none"  stroke="currentColor"  stroke-width="2"  stroke-linecap="round"  stroke-linejoin="round"  class="icon icon-tabler icons-tabler-outline icon-tabler-article"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M3 4m0 2a2 2 0 0 1 2 -2h14a2 2 0 0 1 2 2v12a2 2 0 0 1 -2 2h-14a2 2 0 0 1 -2 -2z" /><path d="M7 8h10" /><path d="M7 12h10" /><path d="M7 16h10" /></svg>'; // 博客页面
            break;
            case 'messages': echo '<svg  xmlns="http://www.w3.org/2000/svg"  class="icon icon-tabler icon-tabler-messages" width="24"  height="24"  viewBox="0 0 24 24"  fill="none"  stroke="currentColor"  stroke-width="2"  stroke-linecap="round"  stroke-linejoin="round"  class="icon icon-tabler icons-tabler-outline icon-tabler-messages"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M21 14l-3 -3h-7a1 1 0 0 1 -1 -1v-6a1 1 0 0 1 1 -1h9a1 1 0 0 1 1 1v10" /><path d="M14 15v2a1 1 0 0 1 -1 1h-7l-3 3v-10a1 1 0 0 1 1 -1h2" /></svg>'; // 留言页面
            break;
            default: echo '<svg  xmlns="http://www.w3.org/2000/svg"  class="icon icon-tabler icon-tabler-file" width="24"  height="24"  viewBox="0 0 24 24"  fill="none"  stroke="currentColor"  stroke-width="2"  stroke-linecap="round"  stroke-linejoin="round"  class="icon icon-tabler icons-tabler-outline icon-tabler-file"><path stroke="none" d="M0 0h24v24H0z" fill="none"/><path d="M14 3v4a1 1 0 0 0 1 1h4" /><path d="M17 21h-10a2 2 0 0 1 -2 -2v-14a2 2 0 0 1 2 -2h7l5 5v11a2 2 0 0 1 -2 2z" /></svg>'; // 默认图标
        } ?>
        <span><?php $pages->title(); ?></span>
        </a>
    </li>
    <?php endwhile; ?>
    老孙
    资深网民孙先生
    1. 王云子

      王云子

      2025-05-06 13:53

      挺会玩儿的小孙~

        1. 老孙

          2025-05-06 13:54
          @王云子

          😎😎 那可不.

    2. acevs

      2025-05-05 16:26

      有个动图演示估计更好,

        1. 老孙

          2025-05-05 16:29
          @acevs

          其实就是折腾Mango主题时候想到的 以后估计还会用到所以就记一下