Typecho 超级标签云,每个标签附带一篇该标签的文章

Typecho教程 2021-09-17 6543℃ 6 1

忘记在哪里看见这个需求了,花了3分钟弄出来。

Typecho都迁移到 PHP 7.2 了,我也采用一个比较优雅的办法弄的。

新来一个类

<?php
class XMetas_Tag_Cloud extends Widget_Metas_Tag_Cloud
{
    /**
     * 获取关联文章
     * @param int $num 文章数量
     * @return Widget_Archive     */
    public function related($num = 1)
    {
        /* @var Widget_Archive $posts */
        $this->widget('Widget_Archive@tag-' . $this->mid, 'pageSize=' . $num . '&type=tag', 'mid=' . $this->mid)->to($posts);
        return $posts;
    }
}

调用方式

<?php /* @var $tags */ $this->widget('XMetas_Tag_Cloud', array('sort' => 'count', 'ignoreZeroCount' => true, 'desc' => true, 'limit' => $pageSize))->to($tags); ?>
<?php if ($tags->have()) : ?>
    <div class="d-gird tags-cloud">
        <div class="row g-2">
            <?php while ($tags->next()) : ?>
                <div class="col-2 col-xl-3 col-lg-4">
                    <div class="tag-item bg-light p-2">
                        <div class="d-flex align-items-center">
                            <a target="_blank" rel="external nofollow"
                               href="<?php $tags->url(); ?>"
                               class="badge bg-white p-2">
                                <?php $tags->name(); ?>
                            </a>
                            <div class="cross mx-2">×</div><?php $tags->count(); ?>                        </div>
                        <?php $related = $tags->related(); ?>
                        <a class="text-ellipsis m-2"
                           href="<?php $related->permalink(); ?>"><?php $related->title(); ?></a>
                    </div>
                </div>
            <?php endwhile; ?>
        </div>
    </div>
<?php endif; ?>

评论 (6)

  • 下一页和部分文章500惊哭

    回复
    • 插件引起的,更新插件了

      回复
  • 我还在用php7.0呢,换7.2主题会报错害羞

    回复
    • 是我的主题吗?

      回复
  • I think everything published was actually very reasonable.

    But, what about this? what if you added a little information? I am not suggesting your information is not
    solid., but suppose you added a title that grabbed folk's attention? I mean Typecho 超级标签云,每个标签附带一篇该标签的文章 -
    虾米皮皮乐 is a little plain. You could look at Yahoo's home page and see how
    they create news titles to grab people to click.
    You might add a video or a pic or two to grab readers excited about what you've written. In my opinion,
    it would make your posts a little bit more interesting.

    回复
  • I have learn a few just right stuff here. Certainly value bookmarking for revisiting.
    I surprise how much effort you put to create this sort of excellent informative site.
    福 步 外贸 论坛 开发 信

    回复
Top