获取指定独立页面

独立页面公开时

在Typecho,以独立页面+模版的形式作为首页,如果想在该页面加上指定页面内容,可以用以下代码实现,以加入about.html内容为例。

    <?php $this->widget('Widget_Contents_Page_List')->to($pages); ?>
    <?php while($pages->next()): ?>
        <?php if ($pages->slug == 'about'): ?>
            <p><?php $pages->content(); ?></p>
                    <?php endif; ?>
    <?php endwhile; ?>

注意:如果该页面是隐藏状态,则引用不可见。

独立页面隐藏时

如果想获取隐藏的独立页面内容,可以用如下代码

<?php
// 假设你想获取的文章cid是123
$cid = 123;
$this->widget('Widget_Archive', 'cid=' . $cid)->to($post);
if ($post->have()) {
    while ($post->next()) {
        // 输出文章内容
        echo '<p>' . $post->content . '</p>';
    }
}
?>

获取当前独立页面内容

在Typecho,独立页面如果应用模版后,就不会显示该页面内容了,如果想同时显示该页面内容,可使用如下代码

<?php
// 获取当前页面的cid
$cid = $this->cid;

// 检查是否有cid,并且不为0(因为0通常是分类页面或首页)
if ($cid > 0) {
    // 使用当前页面的cid创建一个新的Widget_Archive对象
    $this->widget('Widget_Archive', 'cid=' . $cid)->to($post);
    // 输出文章内容
    echo '<p>' . $post->content . '</p>';
}
?>