Files
wordpress-theme-aksrt/templates/pages/page-categories.php
T
kate522 aaf458af1c refactor(autoload): migrate theme classes to Composer PSR-4
Move 288 types from global AKSRT_* + inc/classes manual requires into
namespace AKSRT\{Domain} under src/AKSRT/, wire composer.json psr-4,
and slim bootstrap to helpers/modules/hooks only. Split multi-class
widget/gateway files, update admin contract audit paths, and document
the layout in ADR/README.
2026-07-17 13:29:22 +08:00

140 lines
4.5 KiB
PHP

<?php
/**
* Template Name: AKSRT - Categories
* Template Post Type: page
*
* 分类汇总页面:列出全站所有分类。
*
* 用法:在后台新建一个 Page,标题任意(默认显示页面标题,否则用"分类目录"),
* 模板选择 "AKSRT - Categories";或将 slug 设为 "categories" 自动匹配。
*
* @package AKSRT
*/
defined('ABSPATH') || exit;
get_header();
// ─── 数据准备 ─────────────────────────────────────────────
$all_terms = get_terms([
'taxonomy' => 'category',
'hide_empty' => false, // 全部展示,含 0 篇文章的分类
'orderby' => 'menu_order name',
'order' => 'ASC',
]);
if (is_wp_error($all_terms)) {
$all_terms = [];
}
$top_level = [];
$children_by_parent = [];
foreach ($all_terms as $term) {
if ((int) $term->parent === 0) {
$top_level[] = $term;
} else {
$children_by_parent[(int) $term->parent][] = $term;
}
}
$total_cats = count($all_terms);
$total_counts = wp_count_posts();
$total_posts = $total_counts && isset($total_counts->publish) ? (int) $total_counts->publish : 0;
$page_title = trim((string) get_the_title());
if ($page_title === '') {
$page_title = __('分类目录', 'aksrt');
}
?>
<?php
if (function_exists('aksrt_archive_open_shell')) {
aksrt_archive_open_shell([
'main_class' => 'site-main container content-shell content-shell--page',
'has_sidebar' => false,
'page_kind' => 'categories',
]);
}
?>
<div class="content-main content-main--page content-main--categories">
<?php if (function_exists('aksrt_breadcrumbs')) { aksrt_breadcrumbs(); } ?>
<?php
$cats_meta = sprintf(
/* translators: 1: total category count, 2: total published post count */
esc_html__('共 %1$d 个分类 · 共 %2$d 篇文章', 'aksrt'),
$total_cats,
$total_posts
);
get_template_part('templates/parts/page-header', null, [
'title' => esc_html($page_title),
'desc' => $cats_meta,
]);
?>
<?php if (!empty($top_level)) : ?>
<section class="aksrt-cats-grid" aria-label="<?php esc_attr_e('分类列表', 'aksrt'); ?>">
<?php foreach ($top_level as $cat) :
$cat_id = (int) $cat->term_id;
$cat_link = get_category_link($cat_id);
$cover_url = class_exists('\AKSRT\Settings\Settings')
? \AKSRT\Settings\Settings::get_category_cover_url($cat_id)
: (get_template_directory_uri() . '/assets/img/placeholder.svg');
$desc = class_exists('\AKSRT\Settings\Settings')
? \AKSRT\Settings\Settings::get_category_recommend_desc($cat_id)
: (string) $cat->description;
$children = $children_by_parent[$cat_id] ?? [];
?>
<article class="aksrt-cat-card">
<a class="aksrt-cat-card__cover"
href="<?php echo esc_url($cat_link); ?>"
style="background-image: url('<?php echo esc_url($cover_url); ?>');"
aria-label="<?php echo esc_attr($cat->name); ?>">
<span class="aksrt-cat-card__count-badge">
<?php printf(esc_html__('%d 篇', 'aksrt'), (int) $cat->count); ?>
</span>
</a>
<div class="aksrt-cat-card__body">
<h2 class="aksrt-cat-card__title">
<a href="<?php echo esc_url($cat_link); ?>"><?php echo esc_html($cat->name); ?></a>
</h2>
<?php if ($desc !== '') : ?>
<p class="aksrt-cat-card__desc"><?php echo esc_html(wp_trim_words(wp_strip_all_tags($desc), 40, '…')); ?></p>
<?php endif; ?>
<?php if (!empty($children)) : ?>
<ul class="aksrt-cat-card__children" aria-label="<?php esc_attr_e('子分类', 'aksrt'); ?>">
<?php foreach ($children as $child) : ?>
<li>
<a href="<?php echo esc_url(get_category_link((int) $child->term_id)); ?>">
<span class="aksrt-cat-card__child-name"><?php echo esc_html($child->name); ?></span>
<span class="aksrt-cat-card__child-count"><?php echo (int) $child->count; ?></span>
</a>
</li>
<?php endforeach; ?>
</ul>
<?php endif; ?>
</div>
</article>
<?php endforeach; ?>
</section>
<?php else : ?>
<div class="no-results aksrt-cats-empty">
<p><?php esc_html_e('暂无分类。', 'aksrt'); ?></p>
</div>
<?php endif; ?>
<p class="aksrt-cats-footer">
<a class="btn-outline" href="<?php echo esc_url(home_url('/')); ?>">
<?php esc_html_e('返回首页', 'aksrt'); ?>
</a>
</p>
</div>
<?php
if (function_exists('aksrt_archive_close_shell')) {
aksrt_archive_close_shell(['has_sidebar' => false]);
}
get_footer();