109 lines
3.5 KiB
PHP
109 lines
3.5 KiB
PHP
<?php
|
|
/**
|
|
* Home Module · Tabs
|
|
*
|
|
* config: { cat_ids: int[], count, layout, title? }
|
|
*
|
|
* P6 阶段先实现"按 Tab 标题分组渲染",Tab 切换交互在 P9 阶段引入 JS。
|
|
* 默认行为:渲染选中分类(cat_ids[0])的卡片,其它 Tab 用 data-* 标记,
|
|
* 配合 cms-home-modules.js 切换 active。
|
|
*/
|
|
defined('ABSPATH') || exit;
|
|
|
|
$cfg = get_query_var('aksrt_module_config', []);
|
|
if (!is_array($cfg)) $cfg = [];
|
|
|
|
$cat_ids = isset($cfg['cat_ids']) && is_array($cfg['cat_ids']) ? array_map('intval', $cfg['cat_ids']) : [];
|
|
$cat_ids = array_filter($cat_ids, fn($x) => $x > 0);
|
|
$count = max(1, min(20, (int) ($cfg['count'] ?? 8)));
|
|
$layout = (string) ($cfg['layout'] ?? 'grid-cms');
|
|
$title = (string) ($cfg['title'] ?? '');
|
|
|
|
if (!in_array($layout, ['card', 'list', 'masonry', 'grid-cms'], true)) {
|
|
$layout = 'grid-cms';
|
|
}
|
|
|
|
if (empty($cat_ids)) {
|
|
// 没有指定 → 取站点最热的两个分类
|
|
$top_cats = get_terms([
|
|
'taxonomy' => 'category',
|
|
'orderby' => 'count',
|
|
'order' => 'DESC',
|
|
'number' => 4,
|
|
'hide_empty' => true,
|
|
'fields' => 'ids',
|
|
]);
|
|
if (is_wp_error($top_cats)) $top_cats = [];
|
|
$cat_ids = array_map('intval', $top_cats);
|
|
}
|
|
|
|
if (empty($cat_ids)) return;
|
|
|
|
$tabs = [];
|
|
foreach ($cat_ids as $cid) {
|
|
$term = get_term($cid, 'category');
|
|
if (!$term || is_wp_error($term)) continue;
|
|
$tabs[] = [
|
|
'id' => $cid,
|
|
'name' => $term->name,
|
|
'link' => get_category_link($cid),
|
|
];
|
|
}
|
|
if (empty($tabs)) return;
|
|
|
|
$active_id = $tabs[0]['id'];
|
|
?>
|
|
|
|
<section class="home-module home-module--tabs container home-tab-content index-tab-main" aria-label="<?php echo esc_attr($title ?: __('分类切换', 'aksrt')); ?>">
|
|
<header class="home-module__header home-module__header--tabs">
|
|
<h2 class="home-module__title"><?php echo esc_html($title ?: __('热门分类', 'aksrt')); ?></h2>
|
|
<nav class="home-module__tabs" role="tablist" data-aksrt-tabs>
|
|
<?php foreach ($tabs as $i => $t) : ?>
|
|
<button type="button"
|
|
role="tab"
|
|
class="home-module__tab<?php echo $i === 0 ? ' is-active' : ''; ?>"
|
|
aria-selected="<?php echo $i === 0 ? 'true' : 'false'; ?>"
|
|
data-tab-target="cat-<?php echo (int) $t['id']; ?>">
|
|
<?php echo esc_html($t['name']); ?>
|
|
</button>
|
|
<?php endforeach; ?>
|
|
</nav>
|
|
</header>
|
|
|
|
<?php foreach ($tabs as $i => $t) :
|
|
$tab_post_types = function_exists('aksrt_resource_post_types')
|
|
? array_unique(array_merge(['post'], aksrt_resource_post_types()))
|
|
: ['post'];
|
|
$query = new WP_Query([
|
|
'cat' => $t['id'],
|
|
'post_type' => $tab_post_types,
|
|
'posts_per_page' => $count,
|
|
'post_status' => 'publish',
|
|
'no_found_rows' => true,
|
|
]);
|
|
?>
|
|
<div class="home-module__tab-panel tab-pane<?php echo $i === 0 ? ' is-active' : ''; ?>"
|
|
role="tabpanel"
|
|
data-tab-id="cat-<?php echo (int) $t['id']; ?>"
|
|
<?php echo $i !== 0 ? 'hidden' : ''; ?>>
|
|
<?php if ($query->have_posts()) : ?>
|
|
<div class="posts-row ajaxpager tab-content">
|
|
<?php
|
|
set_query_var('aksrt_post_card_layout', $layout === 'list' ? 'list' : 'card');
|
|
set_query_var('aksrt_post_card_scene', 'home');
|
|
while ($query->have_posts()) {
|
|
$query->the_post();
|
|
get_template_part('templates/parts/content', 'post-card');
|
|
}
|
|
set_query_var('aksrt_post_card_layout', '');
|
|
set_query_var('aksrt_post_card_scene', '');
|
|
wp_reset_postdata();
|
|
?>
|
|
</div>
|
|
<?php else : ?>
|
|
<p class="home-module__empty"><?php esc_html_e('该分类暂无内容', 'aksrt'); ?></p>
|
|
<?php endif; ?>
|
|
</div>
|
|
<?php endforeach; ?>
|
|
</section>
|