85 lines
2.6 KiB
PHP
85 lines
2.6 KiB
PHP
<?php
|
||
/**
|
||
* Home Module · Recent
|
||
*
|
||
* config: { post_type, count, layout, title? }
|
||
*
|
||
* post_type 取值:
|
||
* 'post' → 最新文章
|
||
* 'aksrt_resource' → 最新资源(v3.4 起兼容 post + _aksrt_is_resource)
|
||
*/
|
||
defined('ABSPATH') || exit;
|
||
|
||
$cfg = get_query_var('aksrt_module_config', []);
|
||
if (!is_array($cfg)) $cfg = [];
|
||
|
||
$post_type = (string) ($cfg['post_type'] ?? 'post');
|
||
$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';
|
||
}
|
||
if (!in_array($post_type, ['post', 'aksrt_resource'], true)) $post_type = 'post';
|
||
|
||
$is_resource_module = $post_type === 'aksrt_resource';
|
||
|
||
$args = [
|
||
'posts_per_page' => $count,
|
||
'post_status' => 'publish',
|
||
'no_found_rows' => true,
|
||
'orderby' => 'date',
|
||
'order' => 'DESC',
|
||
];
|
||
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';
|
||
}
|
||
|
||
$query = new WP_Query($args);
|
||
if (!$query->have_posts()) {
|
||
wp_reset_postdata();
|
||
return;
|
||
}
|
||
|
||
$default_title = $is_resource_module ? __('最新资源', 'aksrt') : __('最新文章', 'aksrt');
|
||
$display_title = $title ?: $default_title;
|
||
$archive_link = $is_resource_module
|
||
? (function_exists('aksrt_resource_archive_url') ? aksrt_resource_archive_url() : home_url('/resources/'))
|
||
: 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', '');
|
||
wp_reset_postdata();
|
||
?>
|
||
</div>
|
||
</section>
|