深邃流年 發表於 2023-6-27 00:00:00

DEDECMS 获取当前栏目及所有子栏目的文章数量

<p>
        <span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>以下代码用来解决查询当前栏目及当前栏目下所有子栏目的文章总数,添加到/include/common.func.php </span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>或者 </span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>/include/extend.func.php中,然后在模板中调用getTotalArcByTid(1)。 </span></p>
<div class="msgheader" style='margin: 3px auto 0px; padding: 0px 3px; outline: none; line-height: 21.6px; clear: both; border-width: 1px; border-style: solid; border-color: rgb(0, 153, 204); background: rgb(246, 251, 255); overflow: hidden; font-family: tahoma, arial, "Microsoft YaHei";'>
        <p class="right">
                <span><u>复制代码</u></span></p>
        <p>
                代码如下:</p>
</div>
<p class="msgborder" style='margin: 0px auto 3px; padding: 0px 3px; outline: none; line-height: 25.2px; font-size: 14px; clear: both; border-right: 1px solid rgb(0, 153, 204); background: rgb(221, 237, 251); overflow: hidden; border-left: 1px solid rgb(0, 153, 204); word-break: break-all; border-bottom: 1px solid rgb(0, 153, 204); word-wrap: break-word; font-family: tahoma, arial, "Microsoft YaHei";'>
        <br>
        /* <br>
        * 返回符合记录的文章数量 <br>
        * @description DEDE不允许执行子查询,解决栏目下文章统计的问题 <br>
        * @param $level 为真时查询所有子类目 <br>
        * */ <br>
        function getTotalArcByTid($tid, $level=TRUE) { <br>
        global $dsql; <br>
        $level==TRUE &amp;&amp; $tid = GetSonTypeID($tid); <br>
        $sql = "SELECT count(id) as total from `dede_archives` where typeid in($tid)"; <br>
        $result = $dsql-&gt;GetOne($sql); <br>
        return $result['total']; <br>
        } <br>
        /* <br>
        * 递归获取符合条件的子栏目 <br>
        * @param $tid 栏目ID <br>
        * @return string <br>
        * */ <br>
        function GetSonTypeID($tid) <br>
        { <br>
        global $dsql; <br>
        $dsql-&gt;SetQuery("Select id From `dede_arctype` where reid in($tid) And ishidden&lt;&gt;1 order by sortrank"); <br>
        $dsql-&gt;Execute($tid); <br>
        $typeid = ''; <br>
        while($row=$dsql-&gt;GetObject($tid)) <br>
        { <br>
        $typeid .= "{$row-&gt;id},"; <br>
        $typeid .= GetSonTypeID($row-&gt;id); <br>
        } <br>
        return trim($typeid,','); <br>
        } </p>
<p>
        <br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>调用方法: </span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>模板中调用的方法一般为: </span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>{dede:field.typeid function="getTotalArcByTid(@me)"/} </span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'>或者 </span><br style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'><span style='font-family: tahoma, arial, "Microsoft YaHei"; font-size: 14px;'></span></p>
頁: [1]
查看完整版本: DEDECMS 获取当前栏目及所有子栏目的文章数量