自由调用多个指定cid的文章

之前在网上找的方法都是定死的,而且有些数据调不出来,所以弄了这个,cid参数我是在主题内设置传入的。

/**
 * 指定cid调用多个文章,用于推荐文章调用
 *
 * 方法:$this->widget('Digest_Post@Digests', 'cid=1,2,3')->to($dpost);
 * 只需要传入cid后面的参数即可
 * 在主题内传入 $this->options->digestid,
 * 则调用方式为:$this->widget('Digest_Post@digests', 'cid='.$this->options->digestid)->to($dpost);
 *
 */
class Digest_Post extends Widget_Abstract_Contents
{
    public function execute()
    {

        $this->parameter->setDefault(array('cid' => '0'));
        $getid = explode(',',$this->parameter->cid);
        $pageSize = count($getid);

        $this->db->fetchAll($this->select()
        ->where('table.contents.status = ?', 'publish')
        ->where('table.contents.created < ?', $this->options->time)
        ->where('table.contents.type = ?', 'post')
        ->where('cid in ?',$getid)
        ->limit($pageSize), array($this, 'push'));
    }
}

自定义字段的问题

文档里的写的是

if(isset($this->fields->fieldName)){
  echo '字段存在,值为:'.$this->fields->fieldName;
}else{
  echo '字段不存在';
}

而实际上根本不行,试了下,去掉isset就可以了。

if($this->fields->thumbnail){
    $this->fields->thumbnail();
}else{
    echo 'imgurl';
}

应该是php版本5.x和7.x的原因。

Typecho 用一个程序建多个网站

  1. 首先绑定两个域名(以下称 A域名 和 B域名)到空间目录上。
  2. 接着把typecho上传到空间里,打开 A域名 时,typecho就会自动检测并开始安装,安装是请修改数据库表前缀为 A_ ,安装过程非常顺利。
  3. 安装完后,把程序自动生成的文件config.inc.php下载下来备份,并删除服务器上的config.inc.php。
  4. 然后,用 B域名 打开网站,此时,typecho会再度自动检测并开始安装,此时请修改数据库表前缀为 B_ ,安装过程也一样非常的顺利。
  5. 再次把config.inc.php这个文件下载下来,对比前后两个文件,我们可以发现它们之间的区别就在于最后的数据库及表前缀的区别。
  6. 这两个文件是非常的相似,那么我们就可以模仿wordpress,让它也能一个程序建多个站点了。
if($_SERVER["HTTP_HOST"]=="A域名.com" || $_SERVER["HTTP_HOST"]=="www.A域名.com"){
$db = new Typecho_Db('Mysql', 'A_');
}
else if($_SERVER["HTTP_HOST"]=="B域名.com" || $_SERVER["HTTP_HOST"]=="www.B域名.com"){
$db = new Typecho_Db('Mysql', 'B_');
}
$db->addServer(array (
'host' => 'localhost',
'user' => '数据库用户名',
'password' => '数据库密码',
'charset' => 'utf8',
'port' => '3306',
'database' => '数据库名',
), Typecho_Db::READ | Typecho_Db::WRITE);
Typecho_Db::set($db);'

这样一来,虽然是同一个程序,但当用 A域名 打开时,它调用的是前缀为A_的数据,当用 B域名 打开时,它调用的是前缀为B_的数据

获取模板自定义字段值

前言

在 Typecho 很多模板都要通过设置自定义字段来实现文章缩略图或者其他功能,但是我们在二次开发或者开发插件时,并没有一个接口来实现获取自定义字段,所以便有了我今天的想法。

代码

public function getCustom($cid, $key){
    $db = Typecho_Db::get();
    $rows = $db->fetchAll($db->select('table.fields.str_value')->from('table.fields')
        ->where('table.fields.cid = ?', $cid)
        ->where('table.fields.name = ?', $key)
    );
    // 如果有多个值则存入数组
    foreach ($rows as $row) {
        $img = $row['str_value'];
        if (!empty($img)) {
            $values[] = $img;
        }
    }
    return $values;
}

使用

使用时只要使用 $this->getCustom(mix $cid, mix $key) 就可以了,两个参数分别是文章 cid 和自定义字段名,函数会把自定义内容返回成数组。

来源

https://www.moleft.cn/post-178.html

typecho导航修改的一些问题

当前分类的mid

这个相当不好理解
$this->_pageRow['mid']

不同分类不同模板

也是因为这个才找到了上面那个的解决办法

<?php if (in_array($this->_pageRow['mid'],array(1,8,11,15,16,17,21))) : echo 'caonima';endif; ?>

只显示当前分类的子分类

用于大分类内容的子分类显示,这个就更难了
参考:https://xiamp.net/archives/27.html
把下面的listSubCategories函数放到主题的functions.php就可以用了。

/**
 * 列出子分类
 *
 * @param mixed $category
 * @param mixed $categoryOptions
 * @return void
 */
public static function listSubCategories($category = null, $categoryOptions = null)
{
    if ($category === null) {
        return;
    }

    $widget = Typecho_Widget::widget('Widget_Metas_Category_List');
    $categoryOptions = Typecho_Config::factory($categoryOptions);
    $categoryOptions->setDefault(array(
        'wrapTag' => 'ul',
        'wrapClass' => '',
        'itemTag' => 'li',
        'itemClass' => '',
    ));
    $subCategories = $widget->getCategories($widget->getAllChildren($category));
    if (count($subCategories)) {
        echo "<" . $categoryOptions->wrapTag . (!empty($categoryOptions->wrapClass) ? ' class="' . $categoryOptions->wrapClass . '"' : '') . ">";
        foreach ($subCategories as $subCategory) {
            if ($subCategory['parent'] === $category) { // 不继续寻找子子分类
                echo "<" . $categoryOptions->itemTag . (!empty($categoryOptions->itemClass) ? ' class="' . $categoryOptions->itemClass . '"' : '') . '><a href=' . $subCategory['permalink'] . '>' . $subCategory['name'] . '</a></' . $categoryOptions->itemTag . '>';
            }

        }
        echo "</" . $categoryOptions->wrapTag . ">";
    }
}

使用方式很简单,在archive.php需要列出子分类链接的地方加入以下代码

<?php if($this->is('category')) { listSubCategories($this->_pageRow['mid'], 'wrapClass=no-dots'); }?>

而我测试之后呢,只能显示当前分类的子分类,更下级的分类就不显示了。所以还需要改。
所以就改了文件\var\Widget\Metas\Category\list.php 420行往后

    /**
     * 获取多个分类 
     * 
     * @param mixed $mids 
     * @access public
     * @return array
     */
    public function getCategories($mids)
    {
        $result = array();

        if (!empty($mids)) {
            foreach ($mids as $mid) {
                if (!$this->parameter->ignore
                    || ($this->parameter->ignore != $mid
                    && !$this->hasParent($mid, $this->parameter->ignore))) {
                    $result[] = $this->_map[$mid];
                }
            }
        }

        return $result;
    }

修改为:

    /**
     * 获取多个分类
     *
     * @param mixed $mids
     * @access public
     * @return array
     */
    public function getCategories($mids)
    {
        $result = array();
        $this->parameter->setDefault('view=0');
        $view = $this->parameter->view;
        if (!empty($mids)) {
            foreach ($mids as $mid) {
                if($view) {
                    if ($view == $mid || $this->hasParent($mid, $this->parameter->view)) {
                        $result[] = $this->_map[$mid];
                    }
                } else{
                    if (!$this->parameter->ignore
                        || ($this->parameter->ignore != $mid
                        && !$this->hasParent($mid, $this->parameter->ignore))) {
                        $result[] = $this->_map[$mid];
                    }
                }
            }
        }
        return $result;
    }

调用方法

<?php $this->widget('Widget_Metas_Category_List@options','view=1')->listCategories('wrapClass=topnav'); ?>

先记录备份备用,不一定用的上,还是jquery更强大。

扫一扫,访问移动端

高凯的个人主页
I am alone on my life's Journey up to now, and the future.

推荐 / Digest

热点 / Hot

评论 / Comments

  • Gaobukai: <img class="profile-avat...
  • nico: 头像改为以下函数,还是不起作用。src="<?php av...
  • nico: 现在主题是用下面代码实现src="<?php tx($ne...
  • nico: 博主,我想请教一下。在主题里面怎么调用上传头像。后天正常显示是上...
  • Gaobukai: 这个点赞的按钮为什么总报错呢?
  • Gaobukai: 既然能调用0号图片就能调用随机图片<?php rand(0...

标签 / Tags

推广 / Ads