131 lines
4.1 KiB
PHP
131 lines
4.1 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');
|
|
}
|
|
?>
|
|
|
|
<main id="main-content" class="site-main content-shell" tabindex="-1">
|
|
<?php if (function_exists('aksrt_breadcrumbs')) {
|
|
aksrt_breadcrumbs();
|
|
} ?>
|
|
|
|
<header class="page-header aksrt-cats-header">
|
|
<h1 class="entry-title"><?php echo esc_html($page_title); ?></h1>
|
|
<p class="aksrt-cats-header__meta">
|
|
<?php
|
|
printf(
|
|
/* translators: 1: total category count, 2: total published post count */
|
|
esc_html__('共 %1$d 个分类 · 共 %2$d 篇文章', 'aksrt'),
|
|
$total_cats,
|
|
$total_posts
|
|
);
|
|
?>
|
|
</p>
|
|
</header>
|
|
|
|
<?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')
|
|
? AKSRT_Settings::get_category_cover_url($cat_id)
|
|
: (get_template_directory_uri() . '/assets/img/placeholder.svg');
|
|
$desc = class_exists('AKSRT_Settings')
|
|
? AKSRT_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>
|
|
</main>
|
|
|
|
<?php
|
|
get_footer();
|