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

67 lines
1.8 KiB
PHP

<?php
/**
* Home Module · Recent
*
* config: { count, layout, title? }
* Lists ordinary posts only (no content-type split).
*/
defined('ABSPATH') || exit;
$cfg = get_query_var('aksrt_module_config', []);
if (!is_array($cfg)) {
$cfg = [];
}
$count = max(1, min(20, (int) ($cfg['count'] ?? 6)));
$layout = (string) ($cfg['layout'] ?? 'card');
$title = (string) ($cfg['title'] ?? '');
if (!in_array($layout, ['card', 'list', 'masonry', 'grid-cms'], true)) {
$layout = 'card';
}
$args = [
'post_type' => 'post',
'posts_per_page' => $count,
'post_status' => 'publish',
'no_found_rows' => true,
'orderby' => 'date',
'order' => 'DESC',
];
$query = new WP_Query($args);
if (!$query->have_posts()) {
wp_reset_postdata();
return;
}
$display_title = $title ?: __('最新文章', 'aksrt');
$archive_link = get_permalink(get_option('page_for_posts'));
?>
<section class="home-module home-module--recent container" aria-label="<?php echo esc_attr($display_title); ?>">
<header class="home-module__header">
<h2 class="home-module__title"><?php echo esc_html($display_title); ?></h2>
<?php if ($archive_link) : ?>
<a class="home-module__more" href="<?php echo esc_url($archive_link); ?>">
<?php esc_html_e('查看更多', 'aksrt'); ?> <i class="fas fa-arrow-right" aria-hidden="true"></i>
</a>
<?php endif; ?>
</header>
<div class="post-grid post-grid--<?php echo esc_attr($layout); ?>">
<?php
set_query_var('aksrt_post_card_layout', $layout);
set_query_var('aksrt_post_card_scene', 'home');
while ($query->have_posts()) {
$query->the_post();
get_template_part('templates/parts/content', 'post-card');
}
set_query_var('aksrt_post_card_layout', '');
set_query_var('aksrt_post_card_scene', '');
?>
</div>
</section>
<?php
wp_reset_postdata();