Typecho Wiki
每一个作品都值得被记录

Typecho根据文章cid获取文章信息

Typecho维基君Typecho教程 • 220次浏览 • 发布 2024-02-14 • 更新 2024-02-15
极致加速的V2Ray 助您畅享全球网络 & 搬瓦工VPS最新优惠码
🛜自用大流量超低月租手机卡推荐榜单 #拒绝流量焦虑

早期写Typecho主题时有些文章调用都是用的从数据库直接读取,现在新版Typecho里有些方法被移除了,加上之前的不规范的调用现在已经没法在新版Typecho中使用了,从泽泽那里了解到一个新的文章调用方法,大家可以使用下面代码:

如下代码,其中cid=1就是获取cid为1的文章信息,可以改成你需要获取的文章cid。

<?php $ji=Helper::widgetById('Contents', '1'); ?>
标题:<?php $ji->title(); ?>
链接:<?php $ji->permalink(); ?>
描述:<?php $ji->description(); ?>
文章创建时间:<?php date('Y年m月d日',$ji->created); ?>
文章修改时间:<?php date('Y年m月d日',$ji->modified); ?>

另外这个方法也可以调用分类和tag标签等数据。

参考:https://typecho.work/archives/480.html

如果你不想用上面的方法也可以自己像我一样从content表中直接读取数据自己拼接文章链接:

function getPostUrl($index_url, $cid, $slug, $category, $time){
        $today = date("Y-m-d", $time);
        $todayArr = explode('-', $today);
        $year = $todayArr[0];
        $month = $todayArr[1];
        $day = $todayArr[2];
        $rule = Typecho_Widget::widget('Widget_Options')->routingTable['post']['url'];
        $rule = preg_replace("/\[([_a-z0-9-]+)[^\]]*\]/i", "{\\1}", $rule);
        $part_url = str_replace(array('{cid}', '{slug}', '{category}', '{directory}', '{year}', '{month}', '{day}', '{mid}'),
            array($cid, $slug, $category, '[directory:split:0]',
                $year, $month, $day, 1), $rule);
        $part_url = ltrim($part_url, '/');
        $siteurl = $index_url;
        if (!Typecho_Widget::widget('Widget_Options')->rewrite) {
            $siteurl = $siteurl . 'index.php/';
        }
        $post_url = $siteurl . $part_url;
        return $post_url;
    }

广告声明:文内含有的对外跳转链接(包括不限于超链接、二维码、口令等形式),用于传递更多信息,节省甄选时间,结果仅供参考,Typecho.Wiki所有文章均包含本声明。
本文检索关键词:Typecho教程
厂商投放

【腾讯云】🎉五一云上盛惠!云服务器99元/月续费同价!

腾讯云五一劳动节海量产品 · 轻松上云!云服务器首年1.8折起,买1年送3个月!超值优惠,性能稳定,让您的云端之旅更加畅享。快来腾讯云选购吧!

广告
添加新评论 »