feat(categories): align archive and directory pages
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
7faa2d53d8
commit
fbb53c6890
7 files changed
+443
-315
No files matched your search
@@ -3,137 +3,180 @@
|
||||
* Template Name: Jio - Categories
|
||||
* Template Post Type: page
|
||||
*
|
||||
* 分类汇总页面:列出全站所有分类。
|
||||
*
|
||||
* 用法:在后台新建一个 Page,标题任意(默认显示页面标题,否则用"分类目录"),
|
||||
* 模板选择 "Jio - Categories";或将 slug 设为 "categories" 自动匹配。
|
||||
* Category directory page with recursive taxonomy navigation.
|
||||
*
|
||||
* @package Jio
|
||||
*/
|
||||
|
||||
defined('ABSPATH') || exit;
|
||||
|
||||
get_header();
|
||||
|
||||
// ─── 数据准备 ─────────────────────────────────────────────
|
||||
$post_id = (int) get_queried_object_id();
|
||||
$all_terms = get_terms([
|
||||
'taxonomy' => 'category',
|
||||
'hide_empty' => false, // 全部展示,含 0 篇文章的分类
|
||||
'orderby' => 'menu_order name',
|
||||
'hide_empty' => false,
|
||||
'orderby' => 'term_group',
|
||||
'order' => 'ASC',
|
||||
]);
|
||||
if (is_wp_error($all_terms)) {
|
||||
$all_terms = [];
|
||||
}
|
||||
|
||||
$top_level = [];
|
||||
$top_level = [];
|
||||
$children_by_parent = [];
|
||||
foreach ($all_terms as $term) {
|
||||
if ((int) $term->parent === 0) {
|
||||
$parent_id = (int) $term->parent;
|
||||
if ($parent_id === 0) {
|
||||
$top_level[] = $term;
|
||||
} else {
|
||||
$children_by_parent[(int) $term->parent][] = $term;
|
||||
$children_by_parent[$parent_id][] = $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 = __('分类目录', 'jio');
|
||||
$total_posts = wp_count_posts('post');
|
||||
$total_posts = $total_posts && isset($total_posts->publish) ? (int) $total_posts->publish : 0;
|
||||
$page_title = trim((string) get_the_title($post_id));
|
||||
$page_title = $page_title !== '' ? $page_title : __('分类目录', 'jio');
|
||||
$has_sidebar = function_exists('jio_archive_has_sidebar')
|
||||
? jio_archive_has_sidebar()
|
||||
: (function_exists('jio_is_show_sidebar') ? jio_is_show_sidebar() : false);
|
||||
$content_style = sanitize_key((string) get_post_meta($post_id, 'page_content_style', true));
|
||||
if (!in_array($content_style, ['', 'not', 'nobox', 'full'], true)) {
|
||||
$content_style = '';
|
||||
}
|
||||
?>
|
||||
$content_style_class = $content_style !== '' ? ' page-content-' . $content_style : ' page-content-default';
|
||||
|
||||
$render_children = static function (int $parent_id, array $path) use (&$render_children, $children_by_parent): string {
|
||||
$children = $children_by_parent[$parent_id] ?? [];
|
||||
if ($children === []) {
|
||||
return '';
|
||||
}
|
||||
|
||||
$html = '<ul class="jio-category-directory__children">';
|
||||
foreach ($children as $child) {
|
||||
$child_id = (int) $child->term_id;
|
||||
if (in_array($child_id, $path, true)) {
|
||||
continue;
|
||||
}
|
||||
$link = get_category_link($child_id);
|
||||
if (is_wp_error($link)) {
|
||||
continue;
|
||||
}
|
||||
$description = class_exists('\\Jio\\Settings\\Settings')
|
||||
? \Jio\Settings\Settings::get_category_recommend_desc($child_id)
|
||||
: (string) $child->description;
|
||||
$description = trim(wp_strip_all_tags((string) $description));
|
||||
$html .= '<li class="jio-category-directory__child">';
|
||||
$html .= '<div class="jio-category-directory__child-main"><a href="' . esc_url($link) . '">' . esc_html($child->name) . '</a>';
|
||||
$html .= '<span class="jio-category-directory__count">' . (int) $child->count . '</span></div>';
|
||||
if ($description !== '') {
|
||||
$html .= '<p class="jio-category-directory__child-desc">' . esc_html(wp_trim_words($description, 18, '…')) . '</p>';
|
||||
}
|
||||
$nested = $render_children($child_id, array_merge($path, [$child_id]));
|
||||
if ($nested !== '') {
|
||||
$html .= $nested;
|
||||
}
|
||||
$html .= '</li>';
|
||||
}
|
||||
$html .= '</ul>';
|
||||
return $html;
|
||||
};
|
||||
|
||||
get_header();
|
||||
|
||||
<?php
|
||||
if (function_exists('jio_archive_open_shell')) {
|
||||
jio_archive_open_shell([
|
||||
'main_class' => 'site-main container ',
|
||||
'has_sidebar' => false,
|
||||
'main_class' => 'site-main container' . $content_style_class,
|
||||
'has_sidebar' => $has_sidebar,
|
||||
'top_fluid' => ['page_top_fluid_' . $post_id, 'cat_top_fluid'],
|
||||
'page_kind' => 'categories',
|
||||
]);
|
||||
}
|
||||
?>
|
||||
<div class="content-main content-main--page content-main--categories">
|
||||
<?php if (function_exists('jio_breadcrumbs')) { jio_breadcrumbs(); } ?>
|
||||
<?php
|
||||
$cats_meta = sprintf(
|
||||
/* translators: 1: total category count, 2: total published post count */
|
||||
esc_html__('共 %1$d 个分类 · 共 %2$d 篇文章', 'jio'),
|
||||
$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="jio-cats-grid" aria-label="<?php esc_attr_e('分类列表', 'jio'); ?>">
|
||||
<?php foreach ($top_level as $cat) :
|
||||
$cat_id = (int) $cat->term_id;
|
||||
$cat_link = get_category_link($cat_id);
|
||||
$cover_url = class_exists('\Jio\Settings\Settings')
|
||||
? \Jio\Settings\Settings::get_category_cover_url($cat_id)
|
||||
: (get_template_directory_uri() . '/assets/img/placeholder.svg');
|
||||
$desc = class_exists('\Jio\Settings\Settings')
|
||||
? \Jio\Settings\Settings::get_category_recommend_desc($cat_id)
|
||||
: (string) $cat->description;
|
||||
$children = $children_by_parent[$cat_id] ?? [];
|
||||
?>
|
||||
<article class="jio-cat-card">
|
||||
<a class="jio-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="jio-cat-card__count-badge">
|
||||
<?php printf(esc_html__('%d 篇', 'jio'), (int) $cat->count); ?>
|
||||
</span>
|
||||
</a>
|
||||
|
||||
<div class="jio-cat-card__body">
|
||||
<h2 class="jio-cat-card__title">
|
||||
<a href="<?php echo esc_url($cat_link); ?>"><?php echo esc_html($cat->name); ?></a>
|
||||
</h2>
|
||||
|
||||
<?php if ($desc !== '') : ?>
|
||||
<p class="jio-cat-card__desc"><?php echo esc_html(wp_trim_words(wp_strip_all_tags($desc), 40, '…')); ?></p>
|
||||
<?php if (is_active_sidebar('page_top_content_' . $post_id)) : ?>
|
||||
<?php dynamic_sidebar('page_top_content_' . $post_id); ?>
|
||||
<?php endif; ?>
|
||||
|
||||
<?php if (!empty($children)) : ?>
|
||||
<ul class="jio-cat-card__children" aria-label="<?php esc_attr_e('子分类', 'jio'); ?>">
|
||||
<?php foreach ($children as $child) : ?>
|
||||
<li>
|
||||
<a href="<?php echo esc_url(get_category_link((int) $child->term_id)); ?>">
|
||||
<span class="jio-cat-card__child-name"><?php echo esc_html($child->name); ?></span>
|
||||
<span class="jio-cat-card__child-count"><?php echo (int) $child->count; ?></span>
|
||||
</a>
|
||||
</li>
|
||||
<?php endforeach; ?>
|
||||
</ul>
|
||||
<?php while (have_posts()) : the_post(); ?>
|
||||
<?php if (function_exists('jio_breadcrumbs')) { jio_breadcrumbs(); } ?>
|
||||
<?php
|
||||
get_template_part('templates/parts/page-header', null, [
|
||||
'title' => esc_html($page_title),
|
||||
'desc' => sprintf(
|
||||
/* translators: 1: total category count, 2: total published post count */
|
||||
esc_html__('共 %1$d 个分类 · 共 %2$d 篇文章', 'jio'),
|
||||
count($all_terms),
|
||||
$total_posts
|
||||
),
|
||||
]);
|
||||
if ($content_style !== 'not' && trim((string) get_the_content()) !== '') {
|
||||
echo '<div class="jio-panel"><article class="article wp-posts-content">';
|
||||
the_content();
|
||||
wp_link_pages([
|
||||
'before' => '<p class="text-center post-nav-links radius8 padding-6">',
|
||||
'after' => '</p>',
|
||||
]);
|
||||
echo '</article></div>';
|
||||
}
|
||||
?>
|
||||
|
||||
<?php if (!empty($top_level)) : ?>
|
||||
<nav class="jio-category-directory-nav jio-panel" aria-label="<?php esc_attr_e('分类导航', 'jio'); ?>">
|
||||
<span class="jio-category-directory-nav__label"><?php esc_html_e('分类导航', 'jio'); ?></span>
|
||||
<ul>
|
||||
<?php foreach ($top_level as $term) : ?>
|
||||
<li><a href="#jio-category-<?php echo (int) $term->term_id; ?>"><?php echo esc_html($term->name); ?></a></li>
|
||||
<?php endforeach; ?>
|
||||
</ul>
|
||||
</nav>
|
||||
|
||||
<section class="jio-category-directory" aria-label="<?php esc_attr_e('分类列表', 'jio'); ?>">
|
||||
<?php foreach ($top_level as $term) :
|
||||
$term_id = (int) $term->term_id;
|
||||
$link = get_category_link($term_id);
|
||||
if (is_wp_error($link)) {
|
||||
continue;
|
||||
}
|
||||
$cover = function_exists('jio_widget_term_cover_url')
|
||||
? jio_widget_term_cover_url($term_id, true)
|
||||
: get_template_directory_uri() . '/assets/img/placeholder.svg';
|
||||
$description = class_exists('\Jio\Settings\Settings')
|
||||
? \Jio\Settings\Settings::get_category_recommend_desc($term_id)
|
||||
: (string) $term->description;
|
||||
$description = trim(wp_strip_all_tags((string) $description));
|
||||
?>
|
||||
<article class="jio-category-directory__section" id="jio-category-<?php echo $term_id; ?>">
|
||||
<div class="jio-category-directory__summary">
|
||||
<a class="jio-category-directory__cover" href="<?php echo esc_url($link); ?>" aria-label="<?php echo esc_attr($term->name); ?>">
|
||||
<img <?php echo jio_get_lazy_attr('lazy_other', (string) $cover, 'fit-cover', jio_get_lazy_thumb()); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- helper returns escaped lazy attributes. ?> alt="">
|
||||
</a>
|
||||
<div class="jio-category-directory__intro">
|
||||
<h2><a href="<?php echo esc_url($link); ?>"><?php echo esc_html($term->name); ?></a></h2>
|
||||
<span class="jio-category-directory__count"><?php printf(esc_html__('%d 篇文章', 'jio'), (int) $term->count); ?></span>
|
||||
<?php if ($description !== '') : ?>
|
||||
<p><?php echo esc_html(wp_trim_words($description, 36, '…')); ?></p>
|
||||
<?php endif; ?>
|
||||
<a class="jio-category-directory__more" href="<?php echo esc_url($link); ?>"><?php esc_html_e('查看分类', 'jio'); ?><i class="fa fas fa-angle-right" aria-hidden="true"></i></a>
|
||||
</div>
|
||||
</div>
|
||||
<?php echo $render_children($term_id, [$term_id]); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped -- recursive renderer escapes term data. ?>
|
||||
</article>
|
||||
<?php endforeach; ?>
|
||||
</section>
|
||||
<?php else : ?>
|
||||
<div class="no-results jio-categories-empty"><p><?php esc_html_e('暂无分类。', 'jio'); ?></p></div>
|
||||
<?php endif; ?>
|
||||
<?php endwhile; ?>
|
||||
|
||||
<?php if (is_active_sidebar('page_bottom_content_' . $post_id)) : ?>
|
||||
<?php dynamic_sidebar('page_bottom_content_' . $post_id); ?>
|
||||
<?php endif; ?>
|
||||
</div>
|
||||
</article>
|
||||
<?php endforeach; ?>
|
||||
</section>
|
||||
<?php else : ?>
|
||||
<div class="no-results jio-cats-empty">
|
||||
<p><?php esc_html_e('暂无分类。', 'jio'); ?></p>
|
||||
</div>
|
||||
<?php endif; ?>
|
||||
|
||||
<p class="jio-cats-footer">
|
||||
<a class="btn-outline" href="<?php echo esc_url(home_url('/')); ?>">
|
||||
<?php esc_html_e('返回首页', 'jio'); ?>
|
||||
</a>
|
||||
</p>
|
||||
|
||||
</div>
|
||||
<?php
|
||||
if (function_exists('jio_archive_close_shell')) {
|
||||
jio_archive_close_shell(['has_sidebar' => false]);
|
||||
jio_archive_close_shell([
|
||||
'has_sidebar' => $has_sidebar,
|
||||
'bottom_fluid' => ['cat_bottom_fluid', 'page_bottom_fluid_' . $post_id],
|
||||
]);
|
||||
}
|
||||
|
||||
get_footer();
|
||||
Reference in new issue
Block a user