zblog获取当前文章图片数量的调用方法

时间:5年前   阅读:4046

调用本文总共有几张图片数量代码

下面的示例代码不建议直接复制使用,至少你要把xxx请替换为自己的应用id。

ZBlogPHP开发教程,统计文章中的图片数量,并数量和相册缓存在扩展字段中。

include.php中插入【*请修改后使用,谢谢】

/**
 * 挂载接口
 */function ActivePlugin_xxx() {
    Add_Filter_Plugin('Filter_Plugin_PostArticle_Core', 'xxx_Plugin_PostArticle_Core');}/**
 * 统计文章中的图片数量
 * 
 * @param number $id 文章ID
 * @param object $art 文章对象
 */function xxx_CountArticleImage($id = null, $art = null) {
    global $zbp;
    // 如果设置文章ID 读取文章对象
    if (isset($id) && $id > 0) {
        $art = $zbp->GetPostByID((int) $id);
    }
    // $art文章对象异常时 默认返回0
    if (empty($art) || !$art->ID) {
        return 0;
    }
    // 返回$art->Metas中的缓存数量
    if (isset($art->Metas->images_count)) {
        $return $art->Metas->images_count;
    }
    
    $pattern = "/<img.*?src=[\'|\"](.*?)[\'|\"].*?[\/]?>/i";
    $content = $art->Content;
    preg_match_all($pattern, $content, $matchContent);
    $art->Metas->images_array = implode(',', $matchContent[1]);
    $art->Metas->images_count = count($matchContent[1]);
    $art->Save();
    
    $return $art->Metas->images_count ? $art->Metas->images_count : 0;}/**
 * 提交文章的时候 统计文章中的图片数量
 */function xxx_Plugin_PostArticle_Core(&$article) {
    global $zbp;
    $pattern = "/<img.*?src=[\'|\"](.*?)[\'|\"].*?[\/]?>/i";
    $content = $article->Content;
    preg_match_all($pattern, $content, $matchContent);
    if(!isset($matchContent[1]) || count($matchContent[1]) == 0) {
        return;
    }
    $images = $matchContent[1];
    $image_sum = count($images);
    $article->Metas->images_array = implode(',', $images);
    $article->Metas->images_count = $image_sum;}

如何使用?

{xxx_CountArticleImage(null, $article)}

{xxx_CountArticleImage('文章ID')}

$article需要更换为对象变量

这里的方法缓存了图片的列表,如果不需要去掉这两行代码即可;

$art->Metas->images_array = implode(',', $matchContent[1]);

$article->Metas->images_array = implode(',', $images);

本文内容转载ZBP部落 https://www.zblogphp.com/article/3 

本站声明:网站内容来源于网络,如有侵权,请联系我们https://www.qiquanji.com,我们将及时处理。

微信扫码关注

更新实时通知

上一篇:上证50ETF期权投资技巧干货汇总

下一篇:你有没有中50ETF期权的圈套?

网友评论

请先 登录 再评论,若不是会员请先 注册