导入 AKSRT 主题开发源码
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
<?php
|
||||
/**
|
||||
* Shortcode template: [aksrt_archive]
|
||||
*
|
||||
* 紧凑版文章归档(按年月分组)。供 AKSRT_Shortcodes::render_archive() 通过
|
||||
* AKSRT_Templates::render_archive_shortcode() 以输出缓冲方式 include。
|
||||
*
|
||||
* 可用变量:$attrs(短代码属性)、$content。
|
||||
*
|
||||
* @package AKSRT
|
||||
*/
|
||||
|
||||
defined('ABSPATH') || exit;
|
||||
|
||||
global $wpdb;
|
||||
|
||||
$limit = isset($attrs['limit']) ? max(0, (int) $attrs['limit']) : 0; // 0 = 全部
|
||||
|
||||
$rows = $wpdb->get_results(
|
||||
"SELECT DISTINCT YEAR(post_date) AS year, MONTH(post_date) AS month, COUNT(*) AS count
|
||||
FROM {$wpdb->posts}
|
||||
WHERE post_type = 'post' AND post_status = 'publish'
|
||||
GROUP BY YEAR(post_date), MONTH(post_date)
|
||||
ORDER BY year DESC, month DESC"
|
||||
); // phpcs:ignore WordPress.DB.DirectDatabaseQuery
|
||||
|
||||
if (empty($rows)) {
|
||||
echo '<div class="aksrt-archives aksrt-archives--shortcode aksrt-archives--empty">'
|
||||
. esc_html__('暂无文章。', 'aksrt') . '</div>';
|
||||
return;
|
||||
}
|
||||
|
||||
$current_year = 0;
|
||||
$shown = 0;
|
||||
?>
|
||||
<div class="aksrt-archives aksrt-archives--shortcode">
|
||||
<?php foreach ($rows as $row) :
|
||||
if ((int) $row->year !== $current_year) :
|
||||
$current_year = (int) $row->year;
|
||||
?>
|
||||
<h2 class="aksrt-archives__year"><?php echo esc_html((string) $current_year); ?></h2>
|
||||
<?php endif; ?>
|
||||
<h3 class="aksrt-archives__month"><?php echo esc_html(sprintf(__('%1$d 月(%2$d 篇)', 'aksrt'), (int) $row->month, (int) $row->count)); ?></h3>
|
||||
<ul class="aksrt-archives__list">
|
||||
<?php
|
||||
$month_posts = get_posts([
|
||||
'year' => (int) $row->year,
|
||||
'monthnum' => (int) $row->month,
|
||||
'posts_per_page' => -1,
|
||||
'post_status' => 'publish',
|
||||
'no_found_rows' => true,
|
||||
]);
|
||||
foreach ($month_posts as $mp) :
|
||||
if ($limit > 0 && $shown >= $limit) {
|
||||
break;
|
||||
}
|
||||
$shown++;
|
||||
?>
|
||||
<li class="aksrt-archives__item">
|
||||
<span class="aksrt-archives__date"><?php echo esc_html(get_the_date('m-d', $mp)); ?></span>
|
||||
<a href="<?php echo esc_url(get_permalink($mp->ID)); ?>" class="aksrt-archives__link"><?php echo esc_html(get_the_title($mp)); ?></a>
|
||||
</li>
|
||||
<?php endforeach; ?>
|
||||
</ul>
|
||||
<?php if ($limit > 0 && $shown >= $limit) { break; } ?>
|
||||
<?php endforeach; ?>
|
||||
</div>
|
||||
Reference in New Issue
Block a user