标签云插件

本文展示了通过不同方法实现输出标签云

1.通过官方文档输出标签云

2.通过插件 MyTagCloud 实现标签云

官方 输出标签云

Typecho 默认提供了 Widget_Metas_Tag_Cloud 组件用于标签云展示。

你应该在你的主题的模板文件(比如 sidebar.phpindex.php)中这样写:

编辑文件 sidebar.php

添加

    <?php $this->widget('Widget_Metas_Tag_Cloud', 'sort=count&desc=1&limit=30')->to($tags); ?>
    <?php if($tags->have()): ?>
        <ul class="tag-cloud">
            <?php while($tags->next()): ?>
                <li><a href="<?php $tags->permalink(); ?>"><?php $tags->name(); ?></a></li>
            <?php endwhile; ?>
        </ul>
    <?php endif; ?>

位置

    <?php if (!empty($this->options->sidebarBlock) && in_array('ShowCategory', $this->options->sidebarBlock)): ?>
        <section class="widget">
            <h3 class="widget-title"><?php _e('分类'); ?></h3>
            <?php \Widget\Metas\Category\Rows::alloc()->listCategories('wrapClass=widget-list'); ?>
        </section>
    <?php endif; ?>

    <?php $this->widget('Widget_Metas_Tag_Cloud', 'sort=count&desc=1&limit=30')->to($tags); ?>
    <?php if($tags->have()): ?>
        <ul class="tag-cloud">
            <?php while($tags->next()): ?>
                <li><a href="<?php $tags->permalink(); ?>"><?php $tags->name(); ?></a></li>
            <?php endwhile; ?>
        </ul>
    <?php endif; ?>

    <?php if (!empty($this->options->sidebarBlock) && in_array('ShowArchive', $this->options->sidebarBlock)): ?>
        <section class="widget">
            <h3 class="widget-title"><?php _e('归档'); ?></h3>
            <ul class="widget-list">
                <?php \Widget\Contents\Post\Date::alloc('type=month&format=F Y')
                    ->parse('<li><a href="{permalink}">{date}</a></li>'); ?>
            </ul>
        </section>
    <?php endif; ?>

有效果,但是不是自己想要的,这个是按照无需列表展示的

通过插件 MyTagCloud 实现

使用说明

  1. 下载插件
  2. 将插件上传到/usr/plugins/目录
  3. 在需要使用标签云的模板中放入如下PHP代码 Typecho_Plugin::factory('usr/themes/sidebar.php')->tagCloud();
  4. 登陆后台,在菜单“控制台->插件”中启用插件,并根据自己需求进行配置即可轻松使用
  5. 如果标签云模板结构不同,重写插件中render()方法即可

没有搞懂第三步,启用插件后没有生效,后面通过添加

    <?php if (class_exists('MyTagCloud_Plugin')): ?>
        <?php MyTagCloud_Plugin::process(); ?>
    <?php endif; ?>

到sidebar.php中的分类和归档中生效,是自己想要的效果

实际操作

1.添加插件

2.启用插件,并按照自己需求进行配置

3.调整主题,添加

    <?php if (class_exists('MyTagCloud_Plugin')): ?>
        <?php MyTagCloud_Plugin::process(); ?>
    <?php endif; ?>

到sidebar.php中的分类和归档中

    <?php if (!empty($this->options->sidebarBlock) && in_array('ShowCategory', $this->options->sidebarBlock)): ?>
        <section class="widget">
            <h3 class="widget-title"><?php _e('分类'); ?></h3>
            <?php \Widget\Metas\Category\Rows::alloc()->listCategories('wrapClass=widget-list'); ?>
        </section>
    <?php endif; ?>

    <?php if (class_exists('MyTagCloud_Plugin')): ?>
        <?php MyTagCloud_Plugin::process(); ?>
    <?php endif; ?>

    <?php if (!empty($this->options->sidebarBlock) && in_array('ShowArchive', $this->options->sidebarBlock)): ?>
        <section class="widget">
            <h3 class="widget-title"><?php _e('归档'); ?></h3>
            <ul class="widget-list">
                <?php \Widget\Contents\Post\Date::alloc('type=month&format=F Y')
                    ->parse('<li><a href="{permalink}">{date}</a></li>'); ?>
            </ul>
        </section>
    <?php endif; ?>

有了,是自己想要的样式

标签: typecho, 插件, 个性化

添加新评论