typecho调用随机文章
admin
撰写于 2022年 02月 18 日

typecho调用随机文章

以下是方法

//随机文章

    function getRandomPosts($limit = 4){    
        $db = Typecho_Db::get();
        $result = $db->fetchAll($db->select()->from('table.contents')
            ->where('status = ?','publish')
            ->where('type = ?', 'post')
            ->where('created <= unix_timestamp(now())', 'post')
            ->limit($limit)
            ->order('RAND()')
        );
        if($result){
            $i=1;
            foreach($result as $val){
                if($i<=4){
                    $var = ' class="red"';
                }else{
                    $var = '';
                }
                $val = Typecho_Widget::widget('Widget_Abstract_Contents')->push($val);
                $post_title = htmlspecialchars($val['title']);
                $permalink = $val['permalink'];
                $created = date('Y-m-d', $val['created']);  
                $imgs =thumbside($val['text']);
                echo '<div class="trm-older-publication"><a class="trm-op-top trm-anima-link" href="'.$permalink.'" ><span class="trm-op-cover"><img src="'.$imgs.'" alt="'.$post_title.'"></span><h6 class="trm-op-title">'.$post_title.'</h6>
                              </a>
                            <div class="trm-divider trm-mb-15 trm-mt-20"></div>
                              <ul class="trm-card-data trm-label">
                                <li>'.$created.'</li>
                              </ul>
                            </div>';
    
                $i++;
            }
        }
    }
    /**
     *    
     */

另外加了一个每篇文章的随机图片

    //调用图片
    function thumbside($con) {
    
    preg_match_all('/\<img.*?src\=\"(.*?)\"[^>]*>/i', $con, $thumbUrl);  //通过正则式获取图片地址
    preg_match_all('/\!\[.*?\]\((http(s)?:\/\/.*?(jpg|png))/i', $con, $patternMD);  //通过正则式获取图片地址
    preg_match_all('/\[.*?\]:\s*(http(s)?:\/\/.*?(jpg|png))/i', $con, $patternMDfoot);
    
    
    if(count($thumbUrl[0])>1){
            return $thumbUrl[1][1];  //当找到一个src地址的时候,输出缩略图
    }else if(count($patternMD[0])>1){
            return $patternMD[1][1];
    }else if(count($patternMDfoot[0])>1){
            return $patternMDfoot[1][1];
    }else{
    
    $rand_num = 5; 
    return Helper::options()->siteUrl."usr/themes/Bubble-master/imgs/".rand(1,$rand_num).".jpg?imageMogr2/format/webp";
    }
    }

调用方法

      <?php getRandomPosts();?> 

另外还可以拆分调用 以后再分享出来 最近太忙了

以下方法自己参考调用!!!!

<?php $this->related(3)->to($relatedPosts); ?>    
                   <?php if ($relatedPosts->have()): ?>
                   <?php while ($relatedPosts->next()): ?>
                   <div class="elevate-recirc">
                        <a class="elevate-recirc-img" href="<?php $relatedPosts->permalink(); ?>" ><img class="img" alt="<?php $relatedPosts->title(); ?>" src="<?php $relatedPosts->fields->img(); ?>"></a>
                        <h4><a class="link" href="<?php $relatedPosts->permalink(); ?>" ><?php $relatedPosts->title(); ?></a>
                        </h4>
                        <div class="meta JiEun"><?php $relatedPosts->date('F j, Y'); ?></div>
                    </div>
                    <?php endwhile; ?>
                   <?php else: ?>
                   <?php getRandomPosts();?>
                   <?php endif; ?>    

typecho调用随机文章

typecho调用随机文章

以下是方法

//随机文章

    function getRandomPosts($limit = 4){    
        $db = Typecho_Db::get();
        $result = $db->fetchAll($db->select()->from('table.contents')
            ->where('status = ?','publish')
            ->where('type = ?', 'post')
            ->where('created <= unix_timestamp(now())', 'post')
            ->limit($limit)
            ->order('RAND()')
        );
        if($result){
            $i=1;
            foreach($result as $val){
                if($i<=4){
                    $var = ' class="red"';
                }else{
                    $var = '';
                }
                $val = Typecho_Widget::widget('Widget_Abstract_Contents')->push($val);
                $post_title = htmlspecialchars($val['title']);
                $permalink = $val['permalink'];
                $created = date('Y-m-d', $val['created']);  
                $imgs =thumbside($val['text']);
                echo '<div class="trm-older-publication"><a class="trm-op-top trm-anima-link" href="'.$permalink.'" ><span class="trm-op-cover"><img src="'.$imgs.'" alt="'.$post_title.'"></span><h6 class="trm-op-title">'.$post_title.'</h6>
                              </a>
                            <div class="trm-divider trm-mb-15 trm-mt-20"></div>
                              <ul class="trm-card-data trm-label">
                                <li>'.$created.'</li>
                              </ul>
                            </div>';
    
                $i++;
            }
        }
    }
    /**
     *    
     */

另外加了一个每篇文章的随机图片

    //调用图片
    function thumbside($con) {
    
    preg_match_all('/\<img.*?src\=\"(.*?)\"[^>]*>/i', $con, $thumbUrl);  //通过正则式获取图片地址
    preg_match_all('/\!\[.*?\]\((http(s)?:\/\/.*?(jpg|png))/i', $con, $patternMD);  //通过正则式获取图片地址
    preg_match_all('/\[.*?\]:\s*(http(s)?:\/\/.*?(jpg|png))/i', $con, $patternMDfoot);
    
    
    if(count($thumbUrl[0])>1){
            return $thumbUrl[1][1];  //当找到一个src地址的时候,输出缩略图
    }else if(count($patternMD[0])>1){
            return $patternMD[1][1];
    }else if(count($patternMDfoot[0])>1){
            return $patternMDfoot[1][1];
    }else{
    
    $rand_num = 5; 
    return Helper::options()->siteUrl."usr/themes/Bubble-master/imgs/".rand(1,$rand_num).".jpg?imageMogr2/format/webp";
    }
    }

调用方法

      <?php getRandomPosts();?> 

另外还可以拆分调用 以后再分享出来 最近太忙了

以下方法自己参考调用!!!!

<?php $this->related(3)->to($relatedPosts); ?>    
                   <?php if ($relatedPosts->have()): ?>
                   <?php while ($relatedPosts->next()): ?>
                   <div class="elevate-recirc">
                        <a class="elevate-recirc-img" href="<?php $relatedPosts->permalink(); ?>" ><img class="img" alt="<?php $relatedPosts->title(); ?>" src="<?php $relatedPosts->fields->img(); ?>"></a>
                        <h4><a class="link" href="<?php $relatedPosts->permalink(); ?>" ><?php $relatedPosts->title(); ?></a>
                        </h4>
                        <div class="meta JiEun"><?php $relatedPosts->date('F j, Y'); ?></div>
                    </div>
                    <?php endwhile; ?>
                   <?php else: ?>
                   <?php getRandomPosts();?>
                   <?php endif; ?>    

那年今日
02月
18日
赞 (0)

猜您想看

评论区(暂无评论)

这里空空如也,快来评论吧~

我要评论