Files
wordpress-theme-aksrt/templates/parts/home-modules/module-more.php
T

104 lines
3.3 KiB
PHP

<?php
/**
* Home Module · More (Discover Categories)
*
* Maps to zibll widget-more capability.
* config: { title?, cat_ids[], count, columns, show_post_count, show_latest_posts, latest_count }
*/
defined('ABSPATH') || exit;
$cfg = get_query_var('aksrt_module_config', []);
if (!is_array($cfg)) {
$cfg = [];
}
$title = trim((string) ($cfg['title'] ?? ''));
$cat_ids = isset($cfg['cat_ids']) && is_array($cfg['cat_ids']) ? array_map('absint', $cfg['cat_ids']) : [];
$count = max(1, min(12, (int) ($cfg['count'] ?? 6)));
$columns = max(2, min(4, (int) ($cfg['columns'] ?? 3)));
$show_post_count = !isset($cfg['show_post_count']) || !empty($cfg['show_post_count']);
$show_latest_posts = !isset($cfg['show_latest_posts']) || !empty($cfg['show_latest_posts']);
$latest_count = max(1, min(10, (int) ($cfg['latest_count'] ?? 5)));
// Fetch categories.
if (!empty($cat_ids)) {
$categories = get_categories([
'include' => array_slice($cat_ids, 0, $count),
'orderby' => 'include',
]);
} else {
$categories = get_categories([
'number' => $count,
'orderby' => 'count',
'order' => 'DESC',
'hide_empty' => true,
]);
}
if (empty($categories)) {
return;
}
?>
<section class="home-module home-module--more container" aria-label="<?php echo esc_attr($title ?: __('发现更多', 'aksrt')); ?>">
<header class="home-module__header">
<h2 class="home-module__title"><?php echo esc_html($title ?: __('发现更多', 'aksrt')); ?></h2>
</header>
<div class="home-module__more-grid" style="--cols: <?php echo (int) $columns; ?>;">
<?php foreach ($categories as $cat) :
$cat_link = get_category_link($cat);
$cover = '';
if (function_exists('aksrt_get_category_cover')) {
$cover = aksrt_get_category_cover($cat->term_id);
}
?>
<div class="home-module__more-card">
<a href="<?php echo esc_url($cat_link); ?>" class="home-module__more-link">
<?php if ($cover) : ?>
<div class="home-module__more-cover">
<img<?php echo aksrt_get_lazy_attr('lazy_other', (string) $cover); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?> alt="">
</div>
<?php else : ?>
<div class="home-module__more-cover home-module__more-cover--placeholder">
<i class="fa fas fa-folder" aria-hidden="true"></i>
</div>
<?php endif; ?>
<div class="home-module__more-body">
<strong class="home-module__more-name"><?php echo esc_html($cat->name); ?></strong>
<?php if ($show_post_count) : ?>
<span class="home-module__more-count">
<?php
/* translators: %d: number of posts */
printf(esc_html__('%d 篇文章', 'aksrt'), (int) $cat->count);
?>
</span>
<?php endif; ?>
</div>
</a>
<?php if ($show_latest_posts) :
$recent = get_posts([
'category' => $cat->term_id,
'posts_per_page' => $latest_count,
'post_status' => 'publish',
'orderby' => 'date',
'order' => 'DESC',
]);
if (!empty($recent)) :
?>
<ul class="home-module__more-posts">
<?php foreach ($recent as $rp) : ?>
<li>
<a href="<?php echo esc_url(get_permalink($rp)); ?>"><?php echo esc_html($rp->post_title); ?></a>
</li>
<?php endforeach; ?>
</ul>
<?php
endif;
endif;
?>
</div>
<?php endforeach; ?>
</div>
</section>