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

简介: 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('请输入纯数字')));

前端模板调用

?> widget('Widget_Comments_Recent', "pageSize={this->options->latestComment}")->to(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>

学无止境,谦卑而行.

目录
相关文章
|
缓存 Ubuntu 网络协议
Linux系统各发行版换国内yum或apt源,加速软件下载更新
Centos、Ubuntu、Debian、Fedora、OpenSUSE、FreeBSD系统换软件源
5927 0
|
JavaScript 应用服务中间件 PHP
nginx server 禁止特定目录下的某类文件访问
【8月更文挑战第26天】这段Nginx配置代码旨在保护`/uploads/`目录下的文件,禁止执行任何`.php`, `.html`, `.htm`, 或 `.js`等潜在有害文件,即便被访问也无法运行。取而代之的是重定向到首页。为了实现这一设置,用户需要定位到对应子域名的`.conf`配置文件中进行相应修改。若网站支持多个访问域名,则需确保在正确的`.conf`文件中实施此配置。
772 1
|
PHP
phpmailer中文语言文件及使用
 phpmailer是一个国际化的软件,它也支持中文的出错提示。         phpmailer的语言文件放在language文件夹下,以phpmailer.lang- 语言类型.php命名。
1012 0
|
网络协议 安全
阿里云公共DNS正式发布支持IPv6的版本
在10月23日召开的GNTC 2019全球网络技术大会IPv6分论坛上,阿里云高级技术专家张先国宣布支持阿里公共DNS的IPv6版本正式发布,即阿里公共DNS在保持IPv4 稳定解析服务的基础上(Anycast地址为223.5.5.5,和223.6.6.6),还支持了一组IPv6 Anycast地址为 2400:3200::1和2400:3200:baba::1的资源节点,让处于双栈的用户能够通过配置通过IPv6快速访问DNS。
|
Web App开发 关系型数据库 应用服务中间件
FreeBSD 安装配置Nginx+PHP+APC+MySQL
在 FreeBSD 下安装软件的传统方法是用 ports 源码安装,不过使用 ports 源码编译安装太耗时(尤其是各种库依赖多、大的时候),个人还是喜欢 pkg 这种软件包管理工具直接安装编译好的二进制软件包,不用自己编译,省时省力。
1883 0
|
关系型数据库 Unix Shell
x3d
|
Unix
FreeBSD pkg仓库有台湾的镜像了
http://pkg.freebsd.org/ 在这个页面上可以看到: pkg0.bme.freebsd.org pkg0.nyi.freebsd.org pkg0.twn.freebsd.org pkg0.ydx.freebsd.org 可以在/etc/pkg/FreeBSD.conf中修改服务器地址成你想尝试的地址。
x3d
2554 0