Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
183 lines
7.3 KiB
PHP
183 lines
7.3 KiB
PHP
<?php
|
|
/**
|
|
* Template Name: Jio - Categories
|
|
* Template Post Type: page
|
|
*
|
|
* Category directory page with recursive taxonomy navigation.
|
|
*
|
|
* @package Jio
|
|
*/
|
|
|
|
defined('ABSPATH') || exit;
|
|
|
|
$post_id = (int) get_queried_object_id();
|
|
$all_terms = get_terms([
|
|
'taxonomy' => 'category',
|
|
'hide_empty' => false,
|
|
'orderby' => 'term_group',
|
|
'order' => 'ASC',
|
|
]);
|
|
if (is_wp_error($all_terms)) {
|
|
$all_terms = [];
|
|
}
|
|
|
|
$top_level = [];
|
|
$children_by_parent = [];
|
|
foreach ($all_terms as $term) {
|
|
$parent_id = (int) $term->parent;
|
|
if ($parent_id === 0) {
|
|
$top_level[] = $term;
|
|
} else {
|
|
$children_by_parent[$parent_id][] = $term;
|
|
}
|
|
}
|
|
|
|
$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();
|
|
|
|
if (function_exists('jio_archive_open_shell')) {
|
|
jio_archive_open_shell([
|
|
'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 (is_active_sidebar('page_top_content_' . $post_id)) : ?>
|
|
<?php dynamic_sidebar('page_top_content_' . $post_id); ?>
|
|
<?php endif; ?>
|
|
|
|
<?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>
|
|
<?php
|
|
if (function_exists('jio_archive_close_shell')) {
|
|
jio_archive_close_shell([
|
|
'has_sidebar' => $has_sidebar,
|
|
'bottom_fluid' => ['cat_bottom_fluid', 'page_bottom_fluid_' . $post_id],
|
|
]);
|
|
}
|
|
get_footer();
|