Typecho超级标签云让每个标签附带一篇文章

编辑

忘记在哪里看见这个需求了,花了 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; ?>

出处

文章转自:https://xiamp.net/archives/typecho-super-tag-cloud-each-tag-is-attached-with-an-article-of-the-tag.html

评论区

暂无评论,快来抢沙发