typecho首页的文章怎么仅输出某个分类的文章?

typecho首页的文章怎么仅输出某个分类的文章?

从【Typecho Replica Theme】默认模板改,现在改成如下代码:

    <?php while ($this->next()): ?>
        <article class="post" itemscope itemtype="http://schema.org/BlogPosting">
            <h2 class="post-title" itemprop="name headline">
                <a itemprop="url"
                   href="<?php $this->permalink() ?>"><?php $this->title() ?></a>
            </h2>
            <ul class="post-meta">
                <li><?php _e('时间: '); ?>
                    <time datetime="<?php $this->date('c'); ?>" itemprop="datePublished"><?php $this->date(); ?></time>
                </li>
                <li><?php _e('分类: '); ?><?php $this->category(','); ?></li>
            </ul>
        </article>
    <?php endwhile; ?>

    <?php $this->pageNav('&laquo; 前一页', '后一页 &raquo;'); ?>

不知道从哪里可以改成typecho首页的文章仅输出某个分类的文章?(不想通过插件实现)

阅读 995
1 个回答
<?php
// 获取分类 ID 为 3 的文章列表(你可以根据实际分类 ID 修改 mid=3)
$categoryPosts = $this->widget('Widget_Archive@index_category_news', 'pageSize=10&type=category', 'mid=3');
?>

<?php while ($categoryPosts->next()): ?>
    <article class="post" itemscope itemtype="http://schema.org/BlogPosting">
        <h2 class="post-title" itemprop="name headline">
            <?php $categoryPosts->permalink() ?><?php $categoryPosts->title() ?></a>
        </h2>
        <ul class="post-meta">
            <li><?php _e('时间: '); ?>
                <time datetime="<?php $categoryPosts->date('c'); ?>" itemprop="datePublished"><?php $categoryPosts->date(); ?></time>
            </li>
            <li><?php _e('分类: '); ?><?php $categoryPosts->category(','); ?></li>
        </ul>
    </article>
<?php endwhile; ?>

<?php $categoryPosts->pageNav('&laquo; 前一页', '后一页 &raquo;'); ?>

补充

实现“同分类下的上一篇/下一篇文章”
代码思路(伪代码)

<?php
// 获取当前文章的分类 ID(假设只取第一个分类)
$categories = $this->categories;
$categoryId = $categories[0]['mid'];

// 获取该分类下的所有文章(可以限制数量,比如100篇)
$posts = $this->widget('Widget_Archive@category_posts', 'type=category&pageSize=100', "mid={$categoryId}");

// 遍历查找当前文章的位置
$prev = null;
$next = null;
$found = false;

while ($posts->next()) {
    if ($found) {
        $next = clone $posts;
        break;
    }
    if ($posts->cid == $this->cid) {
        $found = true;
    } else {
        $prev = clone $posts;
    }
}

// 输出链接
if ($prev) {
    echo '<a href="' . $prev->permalink . '">上一篇:' . $prev->title . '</a>';
}
if ($next) {
    echo '<a href="' . $next->permalink . '">下一篇:' . $next->title . '</a>';
}
?>
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进