帝国CMS将正文内容img图片标签里alt值默认设为文章标题

需求:我们在发布内容时,希望内容中的图片都能给alt加上内容,这样对SEO友好,还可能从搜索引擎图片搜索那里带来一些流量,如果人工加,效率低。
问题:正文内容中的图片是和其他文字存在一起,没法用模板标签单独读取出来。
思路:新建一个函数,用于自动替换正文IMG里的ALT内容。
方法:在 e/class/userfun.php 里面增加:

//替换正文IMG里的ALT内容
function user_imgalt($mid,$f,$isadd,$isq,$value,$cs){
$title=$_POST['title'];
$htmls=$value;
$pattern = "/<img[^>]+>/"; 
preg_match_all($pattern, $htmls, $matches); 
for ($i=0; $i<=count($matches[0]); $i++) {
  preg_match_all("/alt=\".+?\"/",$matches[0][$i],$altimg); 
    $t_alt=count($altimg[0]);
    if($t_alt==0){
        $htmls=str_replace("<img","<img alt=\"{$title}\"",$htmls);
    }
}   
    return $htmls;
}

然后在后台系统设置-数据表模型-字段管理里面 编辑 newstext字段,增加如下:
ecms-newstext-imgalt.jpg

本文转自:http://www.dongchuanmin.com/archives/216.html

Typecho纯代码算术验证码

Typecho的垃圾评论还是比较多的,除了插件外,还可以通过PHP函数实现简单的算术验证码。

第一步、function.php如下函数

function themeInit($comment){
$comment = spam_protection_pre($comment, $post, $result);
}
function spam_protection_math(){
    $num1=rand(1,49);
    $num2=rand(1,49);
    echo "<label for=\"math\">请输入<code>$num1</code>+<code>$num2</code>的计算结果:</label>\n";
    echo "<input type=\"text\" name=\"sum\" class=\"text\" value=\"\" size=\"25\" tabindex=\"4\" style=\"width:218px\" placeholder=\"计算结果:\">\n";
    echo "<input type=\"hidden\" name=\"num1\" value=\"$num1\">\n";
    echo "<input type=\"hidden\" name=\"num2\" value=\"$num2\">";
}
function spam_protection_pre($comment, $post, $result){
    $sum=$_POST['sum'];
    switch($sum){
        case $_POST['num1']+$_POST['num2']:
        break;
        case null:
        throw new Typecho_Widget_Exception(_t('对不起: 请输入验证码。<a href="javascript:history.back(-1)">返回上一页</a>','评论失败'));
        break;
        default:
        throw new Typecho_Widget_Exception(_t('对不起: 验证码错误,请<a href="javascript:history.back(-1)">返回</a>重试。','评论失败'));
    }
    return $comment;
}

第二步、comments.php添加函数

打开主题comments.php文件,在适当为止插入如下代码:

<?php spam_protection_math();?>

如果觉得100以内太难了,请修复function.php中添加的代码中rand后面的数字范围。

本文转自:https://minirizhi.com/22.html

Typecho获取随机文章函数(兼容mysql和sqlite)

function getRandomPosts($random=5){
    $db = Typecho_Db::get();
    $adapterName = $db->getAdapterName();//兼容非MySQL数据库
    if($adapterName == 'pgsql' || $adapterName == 'Pdo_Pgsql' || $adapterName == 'Pdo_SQLite' || $adapterName == 'SQLite'){
        $order_by = 'RANDOM()';
    }else{
        $order_by = 'RAND()';
    }
    $sql = $db->select()->from('table.contents')
        ->where('status = ?','publish')
        ->where('table.contents.created <= ?', time())
        ->where('type = ?', 'post')
        ->limit($random)
        ->order($order_by);

$result = $db->fetchAll($sql);
if($result){
    foreach($result as $val){
        $obj = Typecho_Widget::widget('Widget_Abstract_Contents');
        $val = $obj->push($val);
        $post_title = htmlspecialchars($val['title']);
        $permalink = $val['permalink'];
        echo '<a href="'.$permalink.'" title="'.$post_title.'"><h5 class="card-title">'.$post_title.'</h5></a>';
    }
}
}

需要使用时在模板中调用<?php getRandomPosts(10);?>即可,这个随机文章函数的好处就是不光兼容mysql还兼容sqlite数据库。

完整使用方法
1.将上面完整的随机文章代码丢进主题文件夹的function.php里面,保存;
2.在需要添加随机文章的地方加上代码:<?php getRandomPosts(10);?>,保存;
3.刷新页面,搞定!

文章转自:http://www.7tec.cn/246.html

帝国CMS调用栏目别名的两种方法

第一种:在最终栏目调用

<?=$class_r[$GLOBALS[navclassid]][bname]?>

第二种:万能的,哪里都行,应为是直接查询数据库输出的!

<?php 
$cr=$empire->fetch1("select bname from phome_enewsclass where 
classid='".$GLOBALS[navclassid]."' limit 1"); 
echo $cr['bname']; 
?>

使用时,注意上面的表名是否与你的表名对得上,另外classid后面栏目ID,以上案例.$GLOBALS[navclassid].表示获取当前栏目ID。

帝国CMS列表模板list.var使用程序代码

1、增加模板时list.var模板需要勾选“使用程序代码”选项。
2、直接添加PHP代码,不需要加<??>程序开始和结束标记。
3、字段值数组变量为$r,对应的字段变量为$r[字段名],如:标题字段变量就是$r[title]。另外编号变量为$no。
这只是读取当前主表字段,如果是附表,要用副表或者其他表的字段值,要先用SQL取
4、将最终模板内容赋给$listtemp变量。

list.var模板范例:

例子1:如果信息没有设置标题图片就显示指定的图片。

if(empty($r[titlepic]))
{
    $r[titlepic]='/images/img.gif';
}
$listtemp='<li><a href="[!--titleurl--]"><img src="[!--titlepic--]"></a></li>';

说明:$r[titlepic]为标题图片字段变量。$listtemp为模板内容变量。

例子2:如果信息是今天发布的就显示“NEW”图片标识。

$newimg='';
if(time()-$r[newstime]<=1*24*3600)
{
    $newimg='<img src="NEW图片地址" border="0">';
}
$listtemp='<li><a href="[!--titleurl--]">[!--title--]</a> '.$newimg.'</li>';

说明:$r[newstime]为发布时间字段变量。$listtemp为模板内容变量。

例子3:调用投稿用户的公司名称。

$userr=$empire->fetch1("select company from {$dbtbpre}enewsmemberadd where userid='$r[userid]' limit 1");
$listtemp='<li><a href="[!--titleurl--]">[!--title--]</a> <span>公司名称:'.$userr[company].'</span></li>';

说明:$r[userid]为发布者用户ID字段变量。$listtemp为模板内容变量。

其他说明:
如果$listtemp引用模板内容是用单引号,那么里面使用单引号前面要加,例如:$listtemp='<img src=\'[!--titlepic--]\'>';
相反如果引用模板内容是用双引号,那么里面使用双引号前面也要加,例如:$listtemp="<img src=\"[!--titlepic--]\">";

Typecho在PHP7以上版本中出现Database Server Error错误的解决办法

Typecho程序,之前安装的时候是PHP5.X版本,切换PHP7.X以上版本的时候,页面出现 "Database Server Error" 的错误问题。
目前发现这个问题,就搜索了一下,确有解决办法。

config.inc.php可以看到问题了。直接在config.inc.php文件中找到:

$db = new Typecho_Db('Mysql', 'typecho_');

修改成:

$db = new Typecho_Db('Pdo_Mysql', 'typecho_');

替换后,刷新前台、后台均正常使用。