99 lines
2.6 KiB
PHP
99 lines
2.6 KiB
PHP
<?php
|
|
/**
|
|
* Home Module · Ranking
|
|
*
|
|
* config: { metric: 'views|downloads|comments', count, title? }
|
|
* Lists ordinary posts only (no content-type split).
|
|
*/
|
|
defined('ABSPATH') || exit;
|
|
|
|
$cfg = get_query_var('aksrt_module_config', []);
|
|
if (!is_array($cfg)) {
|
|
$cfg = [];
|
|
}
|
|
|
|
$metric = (string) ($cfg['metric'] ?? 'downloads');
|
|
$count = max(1, min(20, (int) ($cfg['count'] ?? 6)));
|
|
$title = (string) ($cfg['title'] ?? '');
|
|
|
|
if (!in_array($metric, ['views', 'downloads', 'comments'], true)) {
|
|
$metric = 'downloads';
|
|
}
|
|
|
|
$args = [
|
|
'post_type' => 'post',
|
|
'posts_per_page' => $count,
|
|
'post_status' => 'publish',
|
|
'no_found_rows' => true,
|
|
];
|
|
|
|
switch ($metric) {
|
|
case 'views':
|
|
$args['meta_key'] = '_aksrt_res_view_count';
|
|
$args['orderby'] = 'meta_value_num';
|
|
$args['order'] = 'DESC';
|
|
break;
|
|
case 'comments':
|
|
$args['orderby'] = 'comment_count';
|
|
$args['order'] = 'DESC';
|
|
break;
|
|
case 'downloads':
|
|
default:
|
|
$args['meta_key'] = '_aksrt_res_download_count';
|
|
$args['orderby'] = 'meta_value_num';
|
|
$args['order'] = 'DESC';
|
|
break;
|
|
}
|
|
|
|
$query = new WP_Query($args);
|
|
if (!$query->have_posts()) {
|
|
wp_reset_postdata();
|
|
return;
|
|
}
|
|
|
|
$metric_label = [
|
|
'views' => __('阅读量', 'aksrt'),
|
|
'downloads' => __('下载量', 'aksrt'),
|
|
'comments' => __('评论数', 'aksrt'),
|
|
][$metric];
|
|
|
|
$display_title = $title ?: sprintf(__('%s 排行榜', 'aksrt'), $metric_label);
|
|
?>
|
|
|
|
<section class="home-module home-module--ranking container" aria-label="<?php echo esc_attr($display_title); ?>">
|
|
<header class="home-module__header">
|
|
<h2 class="home-module__title">
|
|
<i class="fas fa-trophy" aria-hidden="true"></i>
|
|
<?php echo esc_html($display_title); ?>
|
|
</h2>
|
|
</header>
|
|
|
|
<ol class="home-module__ranking-list">
|
|
<?php
|
|
$rank = 1;
|
|
while ($query->have_posts()) :
|
|
$query->the_post();
|
|
$value = 0;
|
|
if ($metric === 'views') {
|
|
$value = (int) get_post_meta(get_the_ID(), '_aksrt_res_view_count', true);
|
|
} elseif ($metric === 'comments') {
|
|
$value = (int) get_comments_number();
|
|
} else {
|
|
$value = (int) get_post_meta(get_the_ID(), '_aksrt_res_download_count', true);
|
|
}
|
|
?>
|
|
<li class="home-module__ranking-item home-module__ranking-item--<?php echo $rank <= 3 ? 'top' : 'normal'; ?>">
|
|
<span class="home-module__ranking-num"><?php echo (int) $rank; ?></span>
|
|
<a class="home-module__ranking-link" href="<?php the_permalink(); ?>">
|
|
<?php the_title(); ?>
|
|
</a>
|
|
<span class="home-module__ranking-value"><?php echo esc_html(number_format_i18n($value)); ?></span>
|
|
</li>
|
|
<?php
|
|
$rank++;
|
|
endwhile;
|
|
wp_reset_postdata();
|
|
?>
|
|
</ol>
|
|
</section>
|