Typecho——如何查看已经提供的方法是否支持传入参数

前言

最近在开发EasyBe主题,打算对侧边一些列表展示数量做成后台可配置的,但是有些列表使用的是typecho本身提供的一些方法,本来打算在function.php中来重写,然后想了下是不是可以看看对应的源码,一看源码才知道原来有些方法是支持传参的;

widget设计文档: https://docs.typecho.org/develop/widget

内容

?> Widget在typecho代码中位于'var/'目录下

查看代码

根据设计文档中的说明,我们可以知道Widget_Comments_Recent代表文件位置是'Widget/Comments/Recent';
所以我们只需要到对应的路径下,找对应的文件,然后查看里面的方法即可;

<?php

namespace Widget\Comments;

use Typecho\Config;
use Typecho\Db;
use Typecho\Db\Exception;
use Widget\Base\Comments;

if (!defined('__TYPECHO_ROOT_DIR__')) {
    exit;
}

/**
 * 最近评论组件
 *
 * @category typecho
 * @package Widget
 * @copyright Copyright (c) 2008 Typecho team (http://www.typecho.org)
 * @license GNU General Public License 2.0
 */
class Recent extends Comments
{
    /**
     * @param Config $parameter
     */
    protected function initParameter(Config $parameter)
    {
        $parameter->setDefault(
            ['pageSize' => $this->options->commentsListSize, 'parentId' => 0, 'ignoreAuthor' => false]
        );
    }

    /**
     * 执行函数
     *
     * @throws Exception
     */
    public function execute()
    {
        $select = $this->select()->limit($this->parameter->pageSize)
            ->where('table.comments.status = ?', 'approved')
            ->order('table.comments.coid', Db::SORT_DESC);

        if ($this->parameter->parentId) {
            $select->where('cid = ?', $this->parameter->parentId);
        }

        if ($this->options->commentsShowCommentOnly) {
            $select->where('type = ?', 'comment');
        }

        /** 忽略作者评论 */
        if ($this->parameter->ignoreAuthor) {
            $select->where('ownerId <> authorId');
        }

        $this->db->fetchAll($select, [$this, 'push']);
    }
}

| 参数 | 说明 |
| ---- | ---- |
| pageSize | 查询数据数量 |
|parentId| 对应文章的ID |
|ignoreAuthor | 是否忽略作者评论 |

前端使用

主要分为两部分,首先在function.php添加对应的配置,其次是在对应的调用方法中进行调用;

后台增加主题配置

!> 在function.php中的themeConfig函数中追加以下代码;

    $latestComment = new Typecho_Widget_Helper_Form_Element_Text('latestComment', NULL, '10', _t('最新评论'), _t('最新评论展示数量'));
    $latestComment->input->setAttribute('class', 'w-60');
    $form->addInput($latestComment->addRule('isInteger', _t('请输入纯数字')));

前端模板调用

?> <?php $this->widget('Widget_Comments_Recent', "pageSize={$this->options->latestComment}")->to($comments); ?>

<!-- 最新评论 -->
                <div id="sidebar_recentcomments" class="sidebar-block">
                    <div class="catListComment">
                        <h3 class="catListTitle">
                            <a href="" class="sidebar-card-title-a">最新评论</a>
                        </h3>
                        <div class="RecentCommentBlock">
                            <ul>
                                <?php if (!empty($this->options->sidebarBlock) && in_array('ShowRecentComments', $this->options->sidebarBlock)): ?>
                                    <?php $this->widget('Widget_Comments_Recent', "pageSize={$this->options->latestComment}")->to($comments); ?>
                                    <?php while($comments->next()): ?>
                                        <li class="recent_comment_title">
                                            <a href="<?php $comments->permalink(); ?>"><?php $comments->title() ?></a>
                                        </li>
                                        <li class="recent_comment_body"><p><?php $comments->excerpt(35, '...'); ?></p></li>
                                        <li class="recent_comment_author">--<?php $comments->author(false); ?></li>
                                    <?php endwhile; ?>
                                <?php endif; ?>
                            </ul>
                        </div>
                    </div>
                </div>
posted @ 2023-02-24 21:41:00 王洋 阅读(643) 评论(0)
发表评论
昵称
邮箱
网址