附件列表

根据文章cid获取文章所有附件的教程之前就写过,但这次我的需求是最新的附件显示在最前面,也就是根据上传时间降序排列,而typecho默认是按照升序排列附件的,看了下typecho源码也有没有自定义的方法,所以就想着采用数组存储每条数组,然后在将数组倒着输出就行了,于是乎就有了下面的例子,例子中只针对了图片,不过原理都是一样的。

<?php
\Widget\Contents\Attachment\Related::alloc(['parentId' => $this->cid])->to($attachment);
$reversed_items = array(); //定义一个空数组
while ($attachment->next()): ?>
    <?php //只有图片附件
    if($attachment->attachment->isImage): ?>
    <?php $reversed_items[]='
        <li class="mt-3" id="att'.$attachment->cid.'" data-image="1"/>
        <img class="rounded-md w-full object-contain"  src="'.$attachment->attachment->url.'">
        <div class="mt-1 flex">
           <button class="rounded-md w-full text-sm text-white py-1 bg-1 hover:bg-sky-600 insert-btn mr-1"
           data-txt="!['.$attachment->title.']('.$attachment->attachment->url.')">插入</button>
           <button class="rounded-md w-full text-sm text-white py-1 bg-4 hover:bg-sky-600 delete-btn" data-cid="'.$attachment->cid.'">删除</button>
        </div>
        </li>';
    ?>
    <?php endif; ?>
<?php endwhile; ?>
<?php 
$reversed_items = array_reverse($reversed_items);  // 翻转新数组
foreach ($reversed_items as $item) {
    echo $item;  // 循环输出倒序的元素
} ?>

反转数组的array_reverse函数是ai告诉我的,否则我可能又自己写for循环从后往前输出了哈哈哈!

版权属于:泽泽社长
本文链接:https://blog.zezeshe.com/archives/attachment-typecho-cid-2.html
本站未注明转载的文章均为原创,并采用 CC BY-NC-SA 4.0 授权协议,转载请注明来源,谢谢!