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

102 lines
2.8 KiB
PHP

<?php
/**
* Home Module · Hotspot 热点推荐
*
* 纯文章列表(无图片),仅桌面端显示。
* config: { count, source, title? }
*
* source:
* 'auto' → 优先读取 aksrt_recommend meta,不足时用最新补充
* 'recent' → 按时间倒序
* 'views' → 按阅读量排序
*/
defined('ABSPATH') || exit;
$cfg = get_query_var('aksrt_module_config', []);
if (!is_array($cfg)) {
$cfg = [];
}
$count = max(3, min(20, (int) ($cfg['count'] ?? 6)));
$source = (string) ($cfg['source'] ?? 'auto');
$title = (string) ($cfg['title'] ?? '');
if (!in_array($source, ['auto', 'recent', 'views'], true)) {
$source = 'auto';
}
$args = [
'post_type' => 'post',
'posts_per_page' => $count,
'post_status' => 'publish',
'no_found_rows' => true,
];
if ($source === 'auto') {
$args['meta_query'] = [
'relation' => 'OR',
['key' => 'aksrt_recommend', 'value' => '1', 'compare' => '='],
['key' => 'aksrt_recommend', 'compare' => 'NOT EXISTS'],
];
$args['orderby'] = 'modified';
$args['order'] = 'DESC';
} elseif ($source === 'views') {
$args['meta_key'] = '_aksrt_res_view_count';
$args['orderby'] = 'meta_value_num';
$args['order'] = 'DESC';
} else {
$args['orderby'] = 'date';
$args['order'] = 'DESC';
}
$query = new WP_Query($args);
if (!$query->have_posts()) {
wp_reset_postdata();
return;
}
$display_title = $title ?: __('热点推荐', 'aksrt');
?>
<section class="home-module home-module--hotspot hotspot--desktop-only" aria-label="<?php echo esc_attr($display_title); ?>">
<header class="home-module__header">
<h2 class="home-module__title">
<i class="fas fa-fire" aria-hidden="true"></i>
<?php echo esc_html($display_title); ?>
</h2>
</header>
<ol class="hotspot__list">
<?php
$rank = 1;
while ($query->have_posts()) :
$query->the_post();
$cats = get_the_category();
$cat_name = $cats ? $cats[0]->name : '';
$views = (int) get_post_meta(get_the_ID(), '_aksrt_res_view_count', true);
?>
<li class="hotspot__item">
<span class="hotspot__num hotspot__num--<?php echo $rank <= 3 ? 'top' : 'normal'; ?>"><?php echo (int) $rank; ?></span>
<div class="hotspot__body">
<a class="hotspot__title" href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
<?php the_title(); ?>
</a>
<div class="hotspot__meta">
<?php if ($cat_name) : ?>
<span class="hotspot__cat"><?php echo esc_html($cat_name); ?></span>
<?php endif; ?>
<span class="hotspot__date"><?php echo esc_html(get_the_date('Y-m-d')); ?></span>
<?php if ($views > 0) : ?>
<span class="hotspot__views"><?php echo esc_html(number_format_i18n($views)); ?> <?php esc_html_e('阅读', 'aksrt'); ?></span>
<?php endif; ?>
</div>
</div>
</li>
<?php
$rank++;
endwhile;
wp_reset_postdata();
?>
</ol>
</section>