导入 AKSRT 主题开发源码
This commit is contained in:
1 parent
9681dd3d48
commit
1268a55dd0
595 files changed
+127705
No files matched your search
@@ -0,0 +1,109 @@
|
||||
<?php
|
||||
/**
|
||||
* Home Module · Ranking
|
||||
*
|
||||
* config: { metric: 'views|downloads|comments', count, post_type, title? }
|
||||
*/
|
||||
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)));
|
||||
$post_type_cfg = (string) ($cfg['post_type'] ?? 'aksrt_resource');
|
||||
$title = (string) ($cfg['title'] ?? '');
|
||||
|
||||
if (!in_array($metric, ['views', 'downloads', 'comments'], true)) {
|
||||
$metric = 'downloads';
|
||||
}
|
||||
|
||||
$is_resource_module = $post_type_cfg === 'aksrt_resource';
|
||||
|
||||
$args = [
|
||||
'posts_per_page' => $count,
|
||||
'post_status' => 'publish',
|
||||
'no_found_rows' => true,
|
||||
];
|
||||
if ($is_resource_module) {
|
||||
$args['post_type'] = function_exists('aksrt_resource_post_types') ? aksrt_resource_post_types() : ['post'];
|
||||
if (!post_type_exists('aksrt_resource')) {
|
||||
$args['meta_query'] = [[
|
||||
'key' => '_aksrt_is_resource',
|
||||
'value' => '1',
|
||||
'compare' => '=',
|
||||
]];
|
||||
}
|
||||
} else {
|
||||
$args['post_type'] = 'post';
|
||||
}
|
||||
|
||||
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>
|
||||
Reference in new issue
Block a user