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

66 lines
1.8 KiB
PHP

<?php
/**
* Home Module · Category
*
* config: { cat_id, count, layout, columns, title? }
*/
defined('ABSPATH') || exit;
$cfg = get_query_var('aksrt_module_config', []);
if (!is_array($cfg)) $cfg = [];
$cat_id = isset($cfg['cat_id']) ? (int) $cfg['cat_id'] : 0;
$count = max(1, min(20, (int) ($cfg['count'] ?? 8)));
$layout = (string) ($cfg['layout'] ?? 'card');
$columns = max(1, min(6, (int) ($cfg['columns'] ?? 4)));
$title = (string) ($cfg['title'] ?? '');
if (!in_array($layout, ['card', 'list'], true)) {
$layout = 'card';
}
$args = [
'post_type' => 'post',
'posts_per_page' => $count,
'post_status' => 'publish',
'no_found_rows' => true,
];
if ($cat_id > 0) {
$args['cat'] = $cat_id;
}
$query = new WP_Query($args);
if (!$query->have_posts()) {
wp_reset_postdata();
return;
}
$display_title = $title;
if ($display_title === '' && $cat_id > 0) {
$term = get_term($cat_id, 'category');
if ($term && !is_wp_error($term)) $display_title = $term->name;
}
$more_link = $cat_id > 0 ? get_category_link($cat_id) : '';
?>
<section class="home-module home-module--category container" aria-label="<?php echo esc_attr($display_title ?: __('分类', 'aksrt')); ?>">
<header class="home-module__header">
<h2 class="home-module__title"><?php echo esc_html($display_title ?: __('精选内容', 'aksrt')); ?></h2>
<?php if ($more_link) : ?>
<a class="home-module__more" href="<?php echo esc_url($more_link); ?>">
<?php esc_html_e('查看更多', 'aksrt'); ?> <i class="fa fas fa-arrow-right" aria-hidden="true"></i>
</a>
<?php endif; ?>
</header>
<div class="posts-row ajaxpager">
<?php
aksrt_render_post_list([
'type' => $layout === 'card' ? 'card' : 'list',
'is_home' => true,
'skip_wrapper' => true,
], $query);
?>
</div>
</section>