Yzmcms 根据栏目id获取栏目下所有文章的数量总和(统计栏目数量功能)

废话不说直接上代码:

此代码直接加到/common/function/extent.func.php中:

<?php

/**

 * extention.func.php   用户自定义函数库

 *

 * @author           袁志蒙  

 * @license          http://www.yzmcms.com

 * @lastmodify       2018-03-18

 */

 //根据栏目id获取包含子分类所有的文章数量

 if(!function_exists('get_count')){

     

     function get_count($catid='0'){

         

         $total = 0;

         

         $catid = intval($catid);

         

         $catinfo = get_category($catid);

         

         $modelid = $catinfo['modelid'];

         

         if($modelid == 0){

            if($catinfo['type'] == 1){ 

                showmsg('此栏目为单页,其下无文章');

            }elseif($catinfo['type'] == 2){

                showmsg('此栏目为外部链接,其下无文章');

            }else{

                showmsg('此模型不存在!');

            }

         }

         

         $table_db = D('model')->where(array('modelid'=>$modelid))->find();

         

         $table_name = $table_db['tablename'];

         

         $arrchildid = $catinfo['arrchildid'];

         

         if($catid === $arrchildid){

            //无子栏目,直接查内容

             $total = D($table_name)->where(array('catid'=>$catid,'status'=>1))->total();

             

         }else{

             //有子栏目,遍历子栏目

            

             $arrchildid = explode(',',$arrchildid);

            

             array_shift($arrchildid);

             

             $count = array();

             

             foreach($arrchildid as $v){

                

                $count[] = D($table_name)->where(array('catid'=>$v,'status'=>1))->total();

                

             }

            

             $total = array_sum($count);

             

         }

        

         return $total;

     

     }

 }

 

 

模板中直接调用get_count(栏目id),即可获取。完美解决


本文关键词:

联系我们

在线咨询:点击这里给我发消息

邮件:w420220301@qq.com